November
Mon | Tues | Wed | Thur | Fri | Sat | Sun |
---|---|---|---|---|---|---|
1 (D1) |
2 (D2) |
3 (D3) |
4 (D4) |
5 (D5) |
6 (D6) |
|
7 (D11) |
8 (D8) |
9 (D9) |
10 (D10) |
11 (D11) |
12 (D12) |
13 (D13) |
14 (D14) |
15 (D15) |
16 (D16) |
17 (D17) |
18 (D18) |
19 (D19) |
20 (D20) |
21 (D21) |
22 (D22) |
23 (D23) |
24 (D24) |
25 (D25) |
26 (D26) |
27 (D27) |
28 (D28) |
29 (D29) |
30 (D30) |
ไปๅคฉๅฌไบ่ๅธไปฌ็ๆฅๅ๏ผๆ่งฆ้ขๅค๏ผไนๅๅญฆไน ่ฟไธๆฎตๆถ้ด็Rust๏ผๅๆถๅๆณๅฏนOSๆ่พไธบๆทฑๅ ฅ็ไบ่งฃ๏ผๅ ๆฌriscvใqemu็ญ็ฅ่ฏ๏ผ้ๅณๅฎๆฅๅๅๅ ็งๅฌๅญฃOS่ฎญ็ป่ฅใ
ๅธๆ้่ฟๆฌๆฌก็่ฎญ็ป่ฅ๏ผๅจๅ่พไปฌ็ๅธฎๅฉไธ๏ผ่ฝๅคๅฏนRustๅOSๆๆด่ฟไธๆญฅ็่ฎค่ฏใ่ฝ็ถๅปๅนดๅฐฑๅ ฅๅไบRust๏ผๅฝๆถๆฏๅ็ๅฎๆน็คพๅบ็TRPL๏ผ่ฝ็ถ่ฟ็จๅพ่ฐ้พ๏ผไฝๆฏrust็้ ็ฝฎๅฎๅจๆฏๆฏc++้ ็ฝฎ่ตทๆฅๅฅฝๅคชๅคไบ๏ผไนๅๅ็ไบrustๅฎๆน็คพๅบ็ไธไบๆๆกฃ๏ผไฝๆปๆฏๆ่ง็ผบๅฐไธ็นๅฎ่ทตใๅ ๆญคไปOS็้กน็ฎๅ่ตท๏ผๅธๆ่ฝๅคๅฏนRustๅๆดๆทฑๅ ฅ็็่งฃใ
้ฆๅ ไปRustlingsๅผๅงๅญฆไน ๏ผ่ฎกๅๅจไธๅจไนๅ ๅฎๆrustlingใไธๆญคๅๆถ๏ผๅจWSL2ไธญ้ ็ฝฎ็ธๅบ็ๅฎ้ช็ฏๅขใ
ไปๅคฉๅฎๆไบRustๅฎ้ช็ฏๅข็้ ็ฝฎใไบๆญคๅๆถ๏ผๅผๅงๅฎๆrustlings็ๅฎ้ช๏ผ็ฎๅๅทฒ็ปๅฎๆ1/3๏ผๅจRust็็งปๅจ่ฏญไน้จๅ่ฟๆฏ่พไธบๅกๅฃณ๏ผๅ ๆญค่ฟ้่ฆๅฏนRust็move semantic่ฟ่ก่ฟไธๆญฅ็ๅญฆไน ไปฅๅ ๆทฑๅฏน็งปๅจ่ฏญไน็็่งฃใ
ๅจquiz1ไธญ๏ผๆไปฌๅฎ็ฐไบไธไธชๆไธบๅบ็ก็่ฎก็ฎไปทๆ ผ็ๅฝๆฐ๏ผไฝๆฏๆณจๆ่ฏฅๅฝๆฐๅช่ฝ่ฟๅ็ฑปๅไธบi32
็ๅผ๏ผๅ ๆญคไธ่ฝ็จไบๅค็่พๅคๆ็ๆ
ๅฝข๏ผๆฏๅฆๅฝ่พๅ
ฅๅผไธๅๆณ๏ผไพๅฆ่ดๆฐ๏ผ๏ผๅ็ปญไผๅญฆๅฐๅฉ็จResult
ๆฅๅค็็ปๆใ
ๅจprimitive_typesไธญ๏ผๆณจๆๅชๆchar
็ฑปๅ็ๅ้ๆ่ฝๅคไฝฟ็จis_numeric()
ๅis_alphabetical()
API.
ๅ
ทไฝๅฏไปฅๅ่link
vector
initialization:
let a = vec![1, 2, 3, 4]; // create vector with 4 values
let b = vec![1; 100]; // in Rust, value first, create a vector with size of 100 and all values equal to 1
let c: Vec<i32> = Vec::new(); // this is also a way to creat the vector
fn zeros(size: u32) -> Vec<i32> {
let mut zero_vec: Vec<i32> = Vec::with_capacity(size as usize); // this is another way to init vec
for i in 0..size {
zero_vec.push(0);
}
zero_vec
}
ๅจRustไธญ๏ผๆVecๅslice็ฑปๅ๏ผๅจไธไพ็vector aไธญ๏ผๅฏไปฅ้่ฟ&a[1..=3]
ๅๅบๅ
ถไธญ็นๅฎ็ๅ็
let a = vec![1, 2, 3, 4, 5];
let new_slice = &a[1..=3];
assert_eq!([2, 3, 4], new_slice);
ๅจvec2ไธญ๏ผๆไธค็งๆนๆณๆน้ไฟฎๆนvec็ๅผ๏ผ
fn vec_loop(mut v: Vec<i32>) -> Vec<i32> {
for in in v.iter_mut() {
*i *= 2;
}
}
fn vec_map(v: &Vec<i32>) -> Vec<i32> {
v.iter().map(|num| {
num * 2 // closure of this
}).collect()
}
ๅจRustไธญ๏ผๅฏนstring
่ฟ่กๆไฝ๏ผtrim()
็จไบๅ ้ค็ฉบๆ ผ๏ผ็ฑปไผผไบPythonไธญ็strip()
link to strings3
fn trim_me(input: &str) -> String {
input.trim().to_string()
}
fn compose_me(input: &str) -> String {
input.to_string() + " world!"
}
fn replace_me(input: &str) -> String {
input.replace("cars", "balloons")
}
// there is also another way to replace the string, that is, use regex
use regex::Regex;
fn replace_regex(input: &str) -> String {
let re = Regex::new(r"[cars]").unwrap();
let res = re.replace_all(input, "balloons");
res
}
Rustไธญ๏ผStringๅstring_sliceๅปๅปๅไธๆธ ๏ผ
fn string_slice(arg: &str) {
println!("{}", arg);
}
fn string(arg: String) {
println!("{}", arg);
}
fn main() {
string_slice("blue");
string("red".to_string());
string("red".into());
string("red".to_owned()); // ้่ฆๆณจๆ็ๆฏ๏ผๅจ็ฑปๅ่ฝฌๆขๆถ๏ผๆๅฅฝไฝฟ็จ`to_owned()`,ไป
ไป
ๅ้
ไธไธชbufferใ
string(String::from("hi"));
string("rust is fun!".to_owned());
string("nice weather".into());
string(format!("Interpolation {}", "Station"));
string_slice(&String::from("abc")[0..1]);
string_slice(" hello there ".trim());
string("Happy Monday!".to_string().replace("Mon", "Fri"));
string("mY sHiFt KeY iS sTicKY".to_lowercase());
}
้่ฆๆณจๆ็ๆฏ๏ผๅจ็ฑปๅ่ฝฌๆขๆถ๏ผๆๅฅฝไฝฟ็จto_owned()
,ไป
ไป
ๅ้
ไธไธชbufferใๅ่
ไปๆฅ่ฟๅบฆ๏ผไปๅคฉๅฎๆไบRustlings็enums๏ผstructs๏ผoptions็ญๅฐ่.
HashMap
in Rust. link to hashmap
let map: HashMap<String, i32> = HashMap::new();
map.insert(String::from("apple"), 3);
map.insert(String::from("mongo"), 2);
// remember the following
for fruit in fruit_kinds {
if !map.contains_key(&fruit) {
map.insert(fruit, 11); // [link to hashmap2]
}
}
ไธ่ฟฐไปฃ็ ๆฎตไธญ็็ฌฌไบ้จๅใlink to hashmap2
I don't know how to implement HashMap3, so just reference to reddit.[link to hashmap3] [reddit refer] [stackoverflow refer]
ๅจRustไธญ๏ผๅฝๅค็Optionsๆถ๏ผไธบไบๆนไพฟๅพๅฐOk()ไธญ็ๅผ๏ผ้ๅธธไฝฟ็จunwrap()
ๆฅ่ทๅพใๆญคๅคๅฏนunwrapๆนๆณๅญ็ใ
link to options3ไธญ๏ผๅฎๅ จไพ้ ็ผ่ฏๅจ็ผ็จใ
Rustไธญ็errors handling่พ้พใ
fn main() -> Result<(), Box<dyn error::error>> {
let pretend_user_input = "32";
let x: i32 = pretend_user_input.parse()?;
println!("output={:?}", PositiveNonzeroInteger::new(x)?);
Ok(())
}
There are two different possible Result
types produced within main()
, which are propagated using ?
operators. ? mark
Under the hood, the ?
operator calls From::from
on the error value to convert it to a boxed trait object, a Box<dyn error::Error>
. This boxed trait object is polymorphic, and since all errors implement the error::Error
trait, we can capture lots of different errors in one "Box" object. boxing errors
ไปๅคฉ้่ฆๅฎๆrustlingsไธญ็ๆๆ็ปไน ้ข๏ผไธๆญคๅๆถ๏ผๅฐ่ฏๅฎๆrustlings็ๆๆๆป็ปใ
ไปๅคฉๅฎๆไบRustlingsไธญthread็ซ ่ไนๅ็ๆๆๅ ๅฎนใๅฏนTrait๏ผไปฅๅไธไบๆบ่ฝๆ้ๆไบไธๅฎ็่ฎค่ฏใ
In macro chapters, remeber the macro should be declared before call. To a certain extent, this is just like C/C++ function call. :)
#[macro_use] // notice the use of #[macro_use]
mod macros {
macro_rules! my_macro {
() => {
println!("Check out my macro!");
};
}
}
fn main() {
my_macro!();
}
Rustไธญ็iterator็็่ฎฉไบบๅคดๅคงใ
fn count_iter(map: &HashMap<String, Progress>, value: Progress) -> usize {
map.values().into_iter().filter(|&v| v == &value).count()
}
fn count_collection_for(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
let mut count = 0;
for map in collection {
for val in map.values() {
if val == &value {
count += 1;
}
}
}
count
}
fn count_collection_iterator(collection: &[HashMap<String, Progress>], value: Progress) -> usize {
collection.iter().map(|m| count_iterator(&m, value)).sum()
}
Rust feature(OOP: T): where T: encapsulation + polymorphism + (inheritance)
- encapsulation
- We have structs and enums to have the
- polymorphism
- Like the trait
๐ All exercises completed! ๐
+----------------------------------------------------+
| You made it to the Fe-nish line! |
+-------------------------- ------------------------+
\\/
โโ โโโโโโโโ โโโโโโโโ โโ
โโโโ โโ โโ โโ โโ โโ โโ โโโโ
โโโโ โโ โโ โโ โโ โโ โโโโ
โโโโโโโโโโ โโ โโ โโ โโโโโโโโ
โโโโโโโโ โโ โโโโ โโ โโโโ โโ โโโโโโโโ
โโโโ โโ โโโโ โโ โโโโ โโโโ โโโโ
โโ โโโโโโ โโโโโโ โโโโโโ โโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโ โโโโโโโโโโโโโโโโโโโโโโโโโโ โโ
โโ โโโโโโโโโโโโโโโโโโโโโโโโโโ โโ
โโ โโ โโโโโโโโโโโโโโโโโโ โโ โโ
โโ โโ โโ โโ โโ โโ
โโ โโ โโ โโ
We hope you enjoyed learning about the various aspects of Rust!
If you noticed any issues, please don't hesitate to report them to our repo.
You can also contribute your own exercises to help the greater community!
Read the xv6 book, Here are some notes of xv6.