├── 01-rust-by-example ├── README.md ├── chapter09-fn │ ├── README.md │ ├── fn_06.rs │ ├── fn_07.rs │ ├── fn_08.rs │ ├── fn_09.rs │ ├── fn_12.rs │ ├── fn_03.rs │ ├── fn_10.rs │ ├── fn_01.rs │ ├── fn_05.rs │ └── fn_11.rs ├── chapter10-mod │ ├── README.md │ ├── my │ │ ├── inaccessible.rs │ │ ├── nested.rs │ │ └── mod.rs │ ├── mod_5.rs │ ├── mod_3.rs │ └── mod_2.rs ├── chapter17-macro │ ├── README.md │ ├── macro_1.rs │ ├── macro_4.rs │ ├── macro_6.rs │ ├── macro_7.rs │ ├── macro_3.rs │ └── macro_2.rs ├── chapter05-types │ ├── README.md │ ├── types_3.rs │ ├── types_4.rs │ ├── types_2.rs │ └── types_1.rs ├── chapter06-conversion │ ├── README.md │ ├── conversion_3.rs │ ├── conversion_1.rs │ └── conversion_2.rs ├── chapter08-flow_control │ ├── README.md │ ├── flow_control_04.rs │ ├── flow_control_03.rs │ ├── flow_control_08.rs │ ├── flow_control_14.rs │ ├── flow_control_12.rs │ ├── flow_control_05.rs │ ├── flow_control_11.rs │ ├── flow_control_02.rs │ ├── flow_control_07.rs │ ├── flow_control_01.rs │ └── flow_control_10.rs ├── chapter11-ceate │ ├── README.md │ ├── executable │ ├── library.rlib │ ├── executable.rs │ ├── Makefile │ └── rary.rs ├── chapter12-cargo │ ├── README.md │ └── foo │ │ ├── src │ │ └── main.rs │ │ ├── Cargo.lock │ │ └── Cargo.toml ├── chapter13-attribute │ ├── README.md │ ├── attribute_4.rs │ ├── Makefile │ ├── attribute_1.rs │ ├── attribute_2.rs │ └── attribute_3.rs ├── chapter14-generics │ ├── README.md │ ├── generics_01.rs │ ├── generics_04.rs │ ├── generics_08.rs │ ├── generics_02.rs │ ├── generics_03.rs │ ├── generics_07.rs │ ├── generics_06.rs │ └── generics_09.rs ├── chapter15-scope │ ├── README.md │ ├── scope_06.rs │ ├── scope_12.rs │ ├── scope_10.rs │ ├── scope_14.rs │ ├── scope_03.rs │ ├── scope_02.rs │ ├── scope_15.rs │ ├── scope_16.rs │ ├── scope_01.rs │ ├── scope_18.rs │ ├── scope_17.rs │ ├── scope_04.rs │ ├── scope_11.rs │ └── scope_13.rs ├── chapter18-error │ ├── README.md │ ├── error_01.rs │ ├── error_05.rs │ ├── error_16.rs │ ├── error_10.rs │ ├── error_09.rs │ ├── error_11.rs │ ├── error_07.rs │ └── error_06_2.rs ├── chapter19-std │ ├── README.md │ └── std_7.rs ├── chapter01-hello │ ├── README.md │ ├── hello_1.rs │ ├── hello_5.rs │ └── hello_3.rs ├── chapter02-primitives │ ├── README.md │ ├── primitive_1.rs │ └── primitive_2.rs ├── chapter03-custom_types │ ├── README.md │ ├── custom_types_6.rs │ ├── custom_types_4.rs │ └── custom_types_3.rs ├── chapter07-experssion │ ├── README.md │ └── experssion.rs ├── chapter16-trait │ ├── README.md │ ├── trait_6.rs │ ├── trait_3.rs │ └── trait_4.rs └── chapter04-variable_bindings │ ├── README.md │ ├── variable_bindings_2.rs │ ├── variable_bindings_4.rs │ ├── variable_bindings_1.rs │ └── variable_bindings_3.rs ├── 06-rcore-lab-notes ├── lab4 │ ├── rust-toolchain │ └── os │ │ ├── src │ │ ├── memory │ │ │ ├── frame │ │ │ │ └── mod.rs │ │ │ ├── mapping │ │ │ │ └── mod.rs │ │ │ └── mod.rs │ │ ├── process │ │ │ ├── config.rs │ │ │ └── mod.rs │ │ ├── algorithm │ │ │ ├── Cargo.toml │ │ │ └── src │ │ │ │ ├── lib.rs │ │ │ │ └── allocator │ │ │ │ └── mod.rs │ │ └── interrupt │ │ │ └── mod.rs │ │ ├── .cargo │ │ └── config │ │ └── Cargo.toml ├── lab5 │ ├── rust-toolchain │ └── os │ │ ├── test.img │ │ ├── src │ │ ├── fs │ │ │ └── config.rs │ │ ├── drivers │ │ │ ├── bus │ │ │ │ └── mod.rs │ │ │ └── mod.rs │ │ ├── memory │ │ │ ├── frame │ │ │ │ └── mod.rs │ │ │ ├── mapping │ │ │ │ └── mod.rs │ │ │ └── mod.rs │ │ ├── process │ │ │ ├── config.rs │ │ │ └── mod.rs │ │ ├── algorithm │ │ │ ├── Cargo.toml │ │ │ └── src │ │ │ │ ├── lib.rs │ │ │ │ └── allocator │ │ │ │ └── mod.rs │ │ └── interrupt │ │ │ └── mod.rs │ │ └── .cargo │ │ └── config ├── lab6 │ ├── rust-toolchain │ ├── user │ │ ├── .cargo │ │ │ └── config │ │ ├── src │ │ │ ├── config.rs │ │ │ └── bin │ │ │ │ ├── hello_world.rs │ │ │ │ └── notebook.rs │ │ └── Cargo.toml │ ├── os │ │ ├── src │ │ │ ├── fs │ │ │ │ └── config.rs │ │ │ ├── drivers │ │ │ │ ├── bus │ │ │ │ │ └── mod.rs │ │ │ │ └── mod.rs │ │ │ ├── memory │ │ │ │ ├── frame │ │ │ │ │ └── mod.rs │ │ │ │ ├── mapping │ │ │ │ │ └── mod.rs │ │ │ │ └── mod.rs │ │ │ ├── process │ │ │ │ ├── config.rs │ │ │ │ └── mod.rs │ │ │ ├── kernel │ │ │ │ ├── process.rs │ │ │ │ ├── mod.rs │ │ │ │ └── condvar.rs │ │ │ ├── algorithm │ │ │ │ ├── Cargo.toml │ │ │ │ └── src │ │ │ │ │ ├── lib.rs │ │ │ │ │ └── allocator │ │ │ │ │ └── mod.rs │ │ │ └── interrupt │ │ │ │ └── mod.rs │ │ └── .cargo │ │ │ └── config │ └── Makefile ├── lab0 │ ├── rust-toolchain │ └── os │ │ ├── Cargo.lock │ │ ├── .cargo │ │ └── config │ │ ├── src │ │ ├── entry.asm │ │ ├── linker.ld │ │ └── panic.rs │ │ └── Cargo.toml ├── lab1 │ ├── rust-toolchain │ └── os │ │ ├── .cargo │ │ └── config │ │ ├── src │ │ ├── entry.asm │ │ ├── interrupt │ │ │ ├── mod.rs │ │ │ └── context.rs │ │ ├── linker.ld │ │ └── panic.rs │ │ └── Cargo.toml ├── lab2 │ ├── rust-toolchain │ └── os │ │ ├── src │ │ ├── memory │ │ │ ├── frame │ │ │ │ └── mod.rs │ │ │ └── mod.rs │ │ ├── algorithm │ │ │ ├── src │ │ │ │ └── lib.rs │ │ │ ├── Cargo.toml │ │ │ └── Cargo.lock │ │ ├── interrupt │ │ │ └── mod.rs │ │ └── entry.asm │ │ ├── .gdb_history │ │ ├── .cargo │ │ └── config │ │ └── Cargo.toml ├── lab3 │ ├── rust-toolchain │ └── os │ │ ├── src │ │ ├── memory │ │ │ ├── frame │ │ │ │ └── mod.rs │ │ │ ├── mapping │ │ │ │ └── mod.rs │ │ │ └── mod.rs │ │ ├── algorithm │ │ │ ├── src │ │ │ │ └── lib.rs │ │ │ └── Cargo.toml │ │ └── interrupt │ │ │ └── mod.rs │ │ ├── .cargo │ │ └── config │ │ └── Cargo.toml ├── lab3_practice │ ├── rust-toolchain │ ├── user │ │ ├── .cargo │ │ │ └── config │ │ ├── src │ │ │ ├── config.rs │ │ │ └── bin │ │ │ │ ├── hello_world.rs │ │ │ │ └── notebook.rs │ │ └── Cargo.toml │ ├── os │ │ ├── src │ │ │ ├── fs │ │ │ │ └── config.rs │ │ │ ├── drivers │ │ │ │ ├── bus │ │ │ │ │ └── mod.rs │ │ │ │ └── mod.rs │ │ │ ├── memory │ │ │ │ ├── frame │ │ │ │ │ └── mod.rs │ │ │ │ ├── mapping │ │ │ │ │ └── mod.rs │ │ │ │ └── mod.rs │ │ │ ├── process │ │ │ │ ├── config.rs │ │ │ │ └── mod.rs │ │ │ ├── algorithm │ │ │ │ ├── src │ │ │ │ │ ├── lib.rs │ │ │ │ │ └── allocator │ │ │ │ │ │ └── mod.rs │ │ │ │ └── Cargo.toml │ │ │ ├── kernel │ │ │ │ ├── process.rs │ │ │ │ ├── mod.rs │ │ │ │ └── condvar.rs │ │ │ └── interrupt │ │ │ │ └── mod.rs │ │ └── .cargo │ │ │ └── config │ └── Makefile ├── lab4_practice_1 │ ├── rust-toolchain │ └── os │ │ ├── src │ │ ├── memory │ │ │ ├── frame │ │ │ │ └── mod.rs │ │ │ └── mapping │ │ │ │ └── mod.rs │ │ ├── process │ │ │ ├── config.rs │ │ │ └── mod.rs │ │ ├── algorithm │ │ │ ├── Cargo.toml │ │ │ ├── src │ │ │ │ ├── lib.rs │ │ │ │ └── allocator │ │ │ │ │ └── mod.rs │ │ │ └── Cargo.lock │ │ └── interrupt │ │ │ └── mod.rs │ │ ├── .cargo │ │ └── config │ │ └── Cargo.toml ├── lab4_practice_2 │ ├── rust-toolchain │ └── os │ │ ├── src │ │ ├── memory │ │ │ ├── frame │ │ │ │ └── mod.rs │ │ │ └── mapping │ │ │ │ └── mod.rs │ │ ├── process │ │ │ ├── config.rs │ │ │ └── mod.rs │ │ ├── algorithm │ │ │ ├── Cargo.toml │ │ │ └── src │ │ │ │ ├── lib.rs │ │ │ │ └── allocator │ │ │ │ └── mod.rs │ │ └── interrupt │ │ │ └── mod.rs │ │ ├── .cargo │ │ └── config │ │ └── Cargo.toml ├── lab6_challenge │ ├── rust-toolchain │ ├── user │ │ ├── .cargo │ │ │ └── config │ │ ├── src │ │ │ ├── config.rs │ │ │ └── bin │ │ │ │ ├── hello_world.rs │ │ │ │ ├── get_tid.rs │ │ │ │ ├── test_fork.rs │ │ │ │ ├── notebook.rs │ │ │ │ └── test_openfile.rs │ │ └── Cargo.toml │ ├── os │ │ ├── src │ │ │ ├── fs │ │ │ │ └── config.rs │ │ │ ├── drivers │ │ │ │ ├── bus │ │ │ │ │ └── mod.rs │ │ │ │ └── mod.rs │ │ │ ├── memory │ │ │ │ ├── frame │ │ │ │ │ └── mod.rs │ │ │ │ └── mapping │ │ │ │ │ └── mod.rs │ │ │ ├── process │ │ │ │ ├── config.rs │ │ │ │ └── mod.rs │ │ │ ├── algorithm │ │ │ │ ├── src │ │ │ │ │ ├── lib.rs │ │ │ │ │ └── allocator │ │ │ │ │ │ └── mod.rs │ │ │ │ └── Cargo.toml │ │ │ ├── kernel │ │ │ │ ├── process.rs │ │ │ │ ├── thread.rs │ │ │ │ └── condvar.rs │ │ │ └── interrupt │ │ │ │ └── mod.rs │ │ └── .cargo │ │ │ └── config │ └── Makefile ├── lab6_practice │ ├── rust-toolchain │ ├── user │ │ ├── .cargo │ │ │ └── config │ │ ├── src │ │ │ ├── config.rs │ │ │ └── bin │ │ │ │ ├── hello_world.rs │ │ │ │ ├── get_tid.rs │ │ │ │ ├── test_fork.rs │ │ │ │ ├── notebook.rs │ │ │ │ └── test_openfile.rs │ │ └── Cargo.toml │ ├── os │ │ ├── src │ │ │ ├── fs │ │ │ │ └── config.rs │ │ │ ├── drivers │ │ │ │ ├── bus │ │ │ │ │ └── mod.rs │ │ │ │ └── mod.rs │ │ │ ├── memory │ │ │ │ ├── frame │ │ │ │ │ └── mod.rs │ │ │ │ └── mapping │ │ │ │ │ └── mod.rs │ │ │ ├── process │ │ │ │ ├── config.rs │ │ │ │ └── mod.rs │ │ │ ├── algorithm │ │ │ │ ├── src │ │ │ │ │ ├── lib.rs │ │ │ │ │ └── allocator │ │ │ │ │ │ └── mod.rs │ │ │ │ └── Cargo.toml │ │ │ ├── kernel │ │ │ │ ├── process.rs │ │ │ │ ├── thread.rs │ │ │ │ └── condvar.rs │ │ │ └── interrupt │ │ │ │ └── mod.rs │ │ └── .cargo │ │ │ └── config │ └── Makefile ├── lab2_practice │ ├── rust-toolchain │ ├── segment_tree_allocator_array │ │ └── Cargo.toml │ └── segment_tree_allocator_pointer │ │ └── Cargo.toml └── README.md ├── 04-learn-rust-the-hard-way ├── README.md ├── ex34.rs ├── ex13.rs ├── ex02.rs ├── ex01.rs ├── ex10.rs ├── ex33.rs ├── ex09.rs ├── ex18.rs ├── ex08.rs ├── ex12.rs ├── ex11.rs ├── ex06.rs ├── ex07.rs ├── ex03.rs ├── ex30.rs └── ex04.rs ├── 02-modular-programing ├── 02-csv-read │ ├── src │ │ └── main.rs │ ├── Cargo.toml │ └── Cargo.lock ├── 07-static_hashmap-2015 │ ├── src │ │ ├── static_func │ │ │ ├── mod.rs │ │ │ ├── static_kv.rs │ │ │ └── read_func.rs │ │ └── main.rs │ ├── Cargo.toml │ └── Cargo.lock ├── 01-csv-read │ ├── src │ │ └── lib.rs │ ├── Cargo.lock │ └── Cargo.toml ├── 09-csv_challenge │ ├── src │ │ ├── core.rs │ │ ├── err.rs │ │ └── opt.rs │ ├── input │ │ └── challenge.csv │ └── Cargo.toml ├── 10-csv_challenge │ ├── src │ │ ├── core.rs │ │ ├── err.rs │ │ ├── lib.rs │ │ └── opt.rs │ ├── input │ │ ├── no_header.csv │ │ └── challenge.csv │ ├── output │ │ └── test.csv │ ├── Cargo.toml │ └── benches │ │ └── file_op_bench.rs ├── 03-use_regex │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 04-use_regex │ ├── Cargo.toml │ └── src │ │ └── main.rs ├── 06-static_hashmap │ ├── Cargo.toml │ └── Cargo.lock ├── 08-static_hashmap-2018 │ ├── Cargo.toml │ ├── Cargo.lock │ └── src │ │ ├── main.rs │ │ ├── read_func │ │ └── static_kv.rs │ │ └── read_func.rs └── 05-use_regex │ └── Cargo.toml ├── 03-rustlilngs-exercises ├── clippy │ ├── Cargo.toml │ ├── Cargo.lock │ ├── clippy2.rs │ ├── clippy1.rs │ └── README.md ├── functions │ ├── functions1.rs │ ├── README.md │ ├── functions5.rs │ ├── functions2.rs │ ├── functions3.rs │ └── functions4.rs ├── variables │ ├── README.md │ ├── variables4.rs │ ├── variables3.rs │ ├── variables5.rs │ ├── variables2.rs │ └── variables1.rs ├── generics │ ├── README.md │ ├── generics1.rs │ └── generics2.rs ├── threads │ └── README.md ├── if │ ├── README.md │ └── if1.rs ├── structs │ └── README.md ├── modules │ ├── README.md │ ├── modules1.rs │ └── modules2.rs ├── error_handling │ └── README.md ├── tests │ ├── README.md │ ├── tests2.rs │ ├── tests1.rs │ └── tests3.rs ├── macros │ ├── macros1.rs │ ├── macros2.rs │ ├── macros4.rs │ ├── macros3.rs │ └── README.md ├── strings │ ├── README.md │ ├── strings1.rs │ └── strings2.rs ├── primitive_types │ ├── primitive_types5.rs │ ├── primitive_types6.rs │ ├── primitive_types4.rs │ ├── README.md │ ├── primitive_types3.rs │ └── primitive_types1.rs ├── option │ ├── README.md │ └── option1.rs ├── enums │ ├── enums1.rs │ ├── README.md │ └── enums2.rs ├── move_semantics │ ├── README.md │ ├── move_semantics1.rs │ ├── move_semantics2.rs │ └── move_semantics3.rs ├── standard_library_types │ └── README.md ├── conversions │ └── using_as.rs └── test4.rs └── 05-leetcode ├── prob09.rs ├── prob07.rs └── prob01.rs /01-rust-by-example/README.md: -------------------------------------------------------------------------------- 1 | # Rust By Example 2 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2020-06-27 -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab5/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2020-06-27 -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2020-06-27 -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab0/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2020-06-27 2 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab1/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2020-06-27 2 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab2/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2020-06-27 2 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2020-06-27 2 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2020-06-27 -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_1/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2020-06-27 -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_2/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2020-06-27 -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2020-06-27 -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2020-06-27 -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab2_practice/rust-toolchain: -------------------------------------------------------------------------------- 1 | nightly-2020-06-27 2 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter09-fn/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 9 函数 2 | 3 | finished at 20200704 4 | 5 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter10-mod/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 10 模块 2 | 3 | finished at 20200704 4 | 5 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter17-macro/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 17 宏 2 | 3 | finished at 20200705 4 | 5 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter05-types/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 5 类型系统 2 | 3 | finished at 20200704 4 | 5 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter06-conversion/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 6 类型转换 2 | 3 | finished at 20200704 4 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter08-flow_control/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 8 流程控制 2 | 3 | finished at 20200704 4 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter11-ceate/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 11 crate 2 | 3 | finished at 20200704 4 | 5 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter12-cargo/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 12 cargo 2 | 3 | finished at 20200704 4 | 5 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter13-attribute/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 13 属性 2 | 3 | finished at 20200704 4 | 5 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter14-generics/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 14 泛型 2 | 3 | finished at 20200704 4 | 5 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter15-scope/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 15 作用域规则 2 | 3 | finished at 20200705 4 | 5 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter18-error/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 18 错误处理 2 | 3 | finished at 20200705 4 | 5 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter19-std/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 19 标准库类型 2 | 3 | finished at 20200705 4 | 5 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter01-hello/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 1: Hello World 2 | 3 | finished at 20200703 4 | 5 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter02-primitives/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 2 原生类型 2 | 3 | finished at 20200703 4 | 5 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter03-custom_types/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 3 自定义类型 2 | 3 | finished at 20200704 4 | 5 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter07-experssion/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 7 表达式 2 | 3 | finished at 20200704 4 | 5 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter12-cargo/foo/src/main.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("Hello, world!"); 3 | } 4 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter16-trait/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 16 特性 trait 2 | 3 | finished at 20200705 4 | 5 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter04-variable_bindings/README.md: -------------------------------------------------------------------------------- 1 | # Chapter 4 变量绑定 2 | 3 | finished at 20200704 4 | 5 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/user/.cargo/config: -------------------------------------------------------------------------------- 1 | # 编译的目标平台 2 | [build] 3 | target = "riscv64imac-unknown-none-elf" 4 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/user/src/config.rs: -------------------------------------------------------------------------------- 1 | /// 每个用户进程所用的堆大小(1M) 2 | pub const USER_HEAP_SIZE: usize = 0x10_0000; 3 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/user/.cargo/config: -------------------------------------------------------------------------------- 1 | # 编译的目标平台 2 | [build] 3 | target = "riscv64imac-unknown-none-elf" 4 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/user/.cargo/config: -------------------------------------------------------------------------------- 1 | # 编译的目标平台 2 | [build] 3 | target = "riscv64imac-unknown-none-elf" 4 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/user/.cargo/config: -------------------------------------------------------------------------------- 1 | # 编译的目标平台 2 | [build] 3 | target = "riscv64imac-unknown-none-elf" 4 | -------------------------------------------------------------------------------- /04-learn-rust-the-hard-way/README.md: -------------------------------------------------------------------------------- 1 | # 笨办法学Rust 2 | 3 | 参考[笨办法学Python](https://www.2cto.com/shouce/Pythonbbf/index.html) 4 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/user/src/config.rs: -------------------------------------------------------------------------------- 1 | /// 每个用户进程所用的堆大小(1M) 2 | pub const USER_HEAP_SIZE: usize = 0x10_0000; 3 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/user/src/config.rs: -------------------------------------------------------------------------------- 1 | /// 每个用户进程所用的堆大小(1M) 2 | pub const USER_HEAP_SIZE: usize = 0x10_0000; 3 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/user/src/config.rs: -------------------------------------------------------------------------------- 1 | /// 每个用户进程所用的堆大小(1M) 2 | pub const USER_HEAP_SIZE: usize = 0x10_0000; 3 | -------------------------------------------------------------------------------- /02-modular-programing/02-csv-read/src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate linked_list; 2 | fn main() { 3 | println!("Hello, world!"); 4 | } 5 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab5/os/test.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yukiiiteru/DailySchedule_2020/HEAD/06-rcore-lab-notes/lab5/os/test.img -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab5/os/src/fs/config.rs: -------------------------------------------------------------------------------- 1 | //! 文件系统的配置信息 2 | 3 | /// 块设备的 Cache 块个数 4 | pub const BLOCK_CACHE_CAPACITY: usize = 0x10; 5 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/os/src/fs/config.rs: -------------------------------------------------------------------------------- 1 | //! 文件系统的配置信息 2 | 3 | /// 块设备的 Cache 块个数 4 | pub const BLOCK_CACHE_CAPACITY: usize = 0x10; 5 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/os/src/fs/config.rs: -------------------------------------------------------------------------------- 1 | //! 文件系统的配置信息 2 | 3 | /// 块设备的 Cache 块个数 4 | pub const BLOCK_CACHE_CAPACITY: usize = 0x10; 5 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/os/src/fs/config.rs: -------------------------------------------------------------------------------- 1 | //! 文件系统的配置信息 2 | 3 | /// 块设备的 Cache 块个数 4 | pub const BLOCK_CACHE_CAPACITY: usize = 0x10; 5 | -------------------------------------------------------------------------------- /02-modular-programing/07-static_hashmap-2015/src/static_func/mod.rs: -------------------------------------------------------------------------------- 1 | // src/static_func/mod.rs 2 | 3 | pub mod static_kv; 4 | pub mod read_func; 5 | 6 | -------------------------------------------------------------------------------- /04-learn-rust-the-hard-way/ex34.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let animals = vec!["bear", "tiger", "penguin", "zebra"]; 3 | let bear = animals[0]; 4 | } 5 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/os/src/fs/config.rs: -------------------------------------------------------------------------------- 1 | //! 文件系统的配置信息 2 | 3 | /// 块设备的 Cache 块个数 4 | pub const BLOCK_CACHE_CAPACITY: usize = 0x10; 5 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter11-ceate/executable: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yukiiiteru/DailySchedule_2020/HEAD/01-rust-by-example/chapter11-ceate/executable -------------------------------------------------------------------------------- /01-rust-by-example/chapter10-mod/my/inaccessible.rs: -------------------------------------------------------------------------------- 1 | pub fn public_function() { 2 | println!("called `my::inaccessible::public_function()`"); 3 | } 4 | 5 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter11-ceate/library.rlib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yukiiiteru/DailySchedule_2020/HEAD/01-rust-by-example/chapter11-ceate/library.rlib -------------------------------------------------------------------------------- /02-modular-programing/01-csv-read/src/lib.rs: -------------------------------------------------------------------------------- 1 | #[cfg(test)] 2 | mod tests { 3 | #[test] 4 | fn it_works() { 5 | assert_eq!(2 + 2, 4); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /04-learn-rust-the-hard-way/ex13.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | 3 | fn main() { 4 | for argument in env::args() { 5 | println!("{}", argument); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/clippy/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "clippy2" 3 | version = "0.0.1" 4 | edition = "2018" 5 | [[bin]] 6 | name = "clippy2" 7 | path = "clippy2.rs" -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab5/os/src/drivers/bus/mod.rs: -------------------------------------------------------------------------------- 1 | //! 总线协议驱动 2 | //! 3 | //! 目前仅仅实现了 virtio MMIO 协议,另外还有类似 PCI 等协议 4 | //! MMIO 指通过读写特定内存段来实现设备交互 5 | 6 | pub mod virtio_mmio; 7 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/os/src/drivers/bus/mod.rs: -------------------------------------------------------------------------------- 1 | //! 总线协议驱动 2 | //! 3 | //! 目前仅仅实现了 virtio MMIO 协议,另外还有类似 PCI 等协议 4 | //! MMIO 指通过读写特定内存段来实现设备交互 5 | 6 | pub mod virtio_mmio; 7 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter01-hello/hello_1.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | // println!("Hello, World!"); 3 | let x = 5 + /* 90 + */ 5; 4 | println!("Is `x` 10 or 100? x = {}", x); 5 | } 6 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/os/src/drivers/bus/mod.rs: -------------------------------------------------------------------------------- 1 | //! 总线协议驱动 2 | //! 3 | //! 目前仅仅实现了 virtio MMIO 协议,另外还有类似 PCI 等协议 4 | //! MMIO 指通过读写特定内存段来实现设备交互 5 | 6 | pub mod virtio_mmio; 7 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/os/src/drivers/bus/mod.rs: -------------------------------------------------------------------------------- 1 | //! 总线协议驱动 2 | //! 3 | //! 目前仅仅实现了 virtio MMIO 协议,另外还有类似 PCI 等协议 4 | //! MMIO 指通过读写特定内存段来实现设备交互 5 | 6 | pub mod virtio_mmio; 7 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab0/os/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "os" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/os/src/drivers/bus/mod.rs: -------------------------------------------------------------------------------- 1 | //! 总线协议驱动 2 | //! 3 | //! 目前仅仅实现了 virtio MMIO 协议,另外还有类似 PCI 等协议 4 | //! MMIO 指通过读写特定内存段来实现设备交互 5 | 6 | pub mod virtio_mmio; 7 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter12-cargo/foo/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "foo" 5 | version = "0.1.0" 6 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter17-macro/macro_1.rs: -------------------------------------------------------------------------------- 1 | macro_rules! say_hello { 2 | () => ( 3 | println!("Hello!"); 4 | ) 5 | } 6 | 7 | fn main() { 8 | say_hello!() 9 | } 10 | 11 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab2/os/src/memory/frame/mod.rs: -------------------------------------------------------------------------------- 1 | //! 物理页的分配与回收 2 | 3 | mod allocator; 4 | mod frame_tracker; 5 | 6 | pub use allocator::FRAME_ALLOCATOR; 7 | pub use frame_tracker::FrameTracker; 8 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3/os/src/memory/frame/mod.rs: -------------------------------------------------------------------------------- 1 | //! 物理页的分配与回收 2 | 3 | mod allocator; 4 | mod frame_tracker; 5 | 6 | pub use allocator::FRAME_ALLOCATOR; 7 | pub use frame_tracker::FrameTracker; 8 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4/os/src/memory/frame/mod.rs: -------------------------------------------------------------------------------- 1 | //! 物理页的分配与回收 2 | 3 | mod allocator; 4 | mod frame_tracker; 5 | 6 | pub use allocator::FRAME_ALLOCATOR; 7 | pub use frame_tracker::FrameTracker; 8 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab5/os/src/memory/frame/mod.rs: -------------------------------------------------------------------------------- 1 | //! 物理页的分配与回收 2 | 3 | mod allocator; 4 | mod frame_tracker; 5 | 6 | pub use allocator::FRAME_ALLOCATOR; 7 | pub use frame_tracker::FrameTracker; 8 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/os/src/memory/frame/mod.rs: -------------------------------------------------------------------------------- 1 | //! 物理页的分配与回收 2 | 3 | mod allocator; 4 | mod frame_tracker; 5 | 6 | pub use allocator::FRAME_ALLOCATOR; 7 | pub use frame_tracker::FrameTracker; 8 | -------------------------------------------------------------------------------- /02-modular-programing/01-csv-read/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "csv-read" 5 | version = "0.1.0" 6 | 7 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/clippy/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "clippy2" 5 | version = "0.0.1" 6 | 7 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab0/os/.cargo/config: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv64imac-unknown-none-elf" 3 | 4 | [target.riscv64imac-unknown-none-elf] 5 | rustflags = [ 6 | "-C", "link-arg=-Tsrc/linker.ld", 7 | ] 8 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab1/os/.cargo/config: -------------------------------------------------------------------------------- 1 | [build] 2 | target = "riscv64imac-unknown-none-elf" 3 | 4 | [target.riscv64imac-unknown-none-elf] 5 | rustflags = [ 6 | "-C", "link-arg=-Tsrc/linker.ld", 7 | ] 8 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/os/src/memory/frame/mod.rs: -------------------------------------------------------------------------------- 1 | //! 物理页的分配与回收 2 | 3 | mod allocator; 4 | mod frame_tracker; 5 | 6 | pub use allocator::FRAME_ALLOCATOR; 7 | pub use frame_tracker::FrameTracker; 8 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_1/os/src/memory/frame/mod.rs: -------------------------------------------------------------------------------- 1 | //! 物理页的分配与回收 2 | 3 | mod allocator; 4 | mod frame_tracker; 5 | 6 | pub use allocator::FRAME_ALLOCATOR; 7 | pub use frame_tracker::FrameTracker; 8 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_2/os/src/memory/frame/mod.rs: -------------------------------------------------------------------------------- 1 | //! 物理页的分配与回收 2 | 3 | mod allocator; 4 | mod frame_tracker; 5 | 6 | pub use allocator::FRAME_ALLOCATOR; 7 | pub use frame_tracker::FrameTracker; 8 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/os/src/memory/frame/mod.rs: -------------------------------------------------------------------------------- 1 | //! 物理页的分配与回收 2 | 3 | mod allocator; 4 | mod frame_tracker; 5 | 6 | pub use allocator::FRAME_ALLOCATOR; 7 | pub use frame_tracker::FrameTracker; 8 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/os/src/memory/frame/mod.rs: -------------------------------------------------------------------------------- 1 | //! 物理页的分配与回收 2 | 3 | mod allocator; 4 | mod frame_tracker; 5 | 6 | pub use allocator::FRAME_ALLOCATOR; 7 | pub use frame_tracker::FrameTracker; 8 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab2/os/src/algorithm/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! 一些可能用到,而又不好找库的数据结构 2 | //! 3 | //! 以及有多种实现,会留作业的数据结构 4 | #![no_std] 5 | 6 | extern crate alloc; 7 | 8 | mod allocator; 9 | 10 | pub use allocator::*; 11 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3/os/src/algorithm/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! 一些可能用到,而又不好找库的数据结构 2 | //! 3 | //! 以及有多种实现,会留作业的数据结构 4 | #![no_std] 5 | 6 | extern crate alloc; 7 | 8 | mod allocator; 9 | 10 | pub use allocator::*; 11 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4/os/src/process/config.rs: -------------------------------------------------------------------------------- 1 | //! 定义一些进程相关的常量 2 | 3 | /// 每个线程的运行栈大小 512 KB 4 | pub const STACK_SIZE: usize = 0x8_0000; 5 | 6 | /// 共用的内核栈大小 512 KB 7 | pub const KERNEL_STACK_SIZE: usize = 0x8_0000; 8 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab5/os/src/process/config.rs: -------------------------------------------------------------------------------- 1 | //! 定义一些进程相关的常量 2 | 3 | /// 每个线程的运行栈大小 512 KB 4 | pub const STACK_SIZE: usize = 0x8_0000; 5 | 6 | /// 共用的内核栈大小 512 KB 7 | pub const KERNEL_STACK_SIZE: usize = 0x8_0000; 8 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/os/src/process/config.rs: -------------------------------------------------------------------------------- 1 | //! 定义一些进程相关的常量 2 | 3 | /// 每个线程的运行栈大小 512 KB 4 | pub const STACK_SIZE: usize = 0x8_0000; 5 | 6 | /// 共用的内核栈大小 512 KB 7 | pub const KERNEL_STACK_SIZE: usize = 0x8_0000; 8 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter11-ceate/executable.rs: -------------------------------------------------------------------------------- 1 | extern crate rary; 2 | 3 | fn main() { 4 | rary::public_function(); 5 | 6 | // rary::private_function(); 7 | 8 | rary::indirect_access(); 9 | } 10 | 11 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter13-attribute/attribute_4.rs: -------------------------------------------------------------------------------- 1 | #[cfg(some_condition)] 2 | fn conditional_function() { 3 | println!("condition met!") 4 | } 5 | 6 | fn main() { 7 | conditional_function(); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter10-mod/my/nested.rs: -------------------------------------------------------------------------------- 1 | pub fn function() { 2 | println!("called `my::nested::function()`"); 3 | } 4 | 5 | fn private_function() { 6 | println!("called `my::nested::private_function()`"); 7 | } 8 | 9 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/os/src/process/config.rs: -------------------------------------------------------------------------------- 1 | //! 定义一些进程相关的常量 2 | 3 | /// 每个线程的运行栈大小 512 KB 4 | pub const STACK_SIZE: usize = 0x8_0000; 5 | 6 | /// 共用的内核栈大小 512 KB 7 | pub const KERNEL_STACK_SIZE: usize = 0x8_0000; 8 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/os/src/process/config.rs: -------------------------------------------------------------------------------- 1 | //! 定义一些进程相关的常量 2 | 3 | /// 每个线程的运行栈大小 512 KB 4 | pub const STACK_SIZE: usize = 0x8_0000; 5 | 6 | /// 共用的内核栈大小 512 KB 7 | pub const KERNEL_STACK_SIZE: usize = 0x8_0000; 8 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/os/src/process/config.rs: -------------------------------------------------------------------------------- 1 | //! 定义一些进程相关的常量 2 | 3 | /// 每个线程的运行栈大小 512 KB 4 | pub const STACK_SIZE: usize = 0x8_0000; 5 | 6 | /// 共用的内核栈大小 512 KB 7 | pub const KERNEL_STACK_SIZE: usize = 0x8_0000; 8 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter05-types/types_3.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let elem = 5u8; 3 | 4 | let mut vec = Vec::new(); 5 | vec.push(elem); // without this line, error will occur at line 6 6 | println!("{:?}", vec); 7 | } 8 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter09-fn/fn_06.rs: -------------------------------------------------------------------------------- 1 | fn apply(f: F) where 2 | F: Fn() { 3 | f(); 4 | } 5 | 6 | fn main() { 7 | let x = 7; 8 | let print = || println!("{}", x); 9 | 10 | apply(print); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter13-attribute/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | rustc attribute_4.rs 3 | 4 | cfg: 5 | rustc attribute_4.rs --cfg some_condition 6 | 7 | run:attribute_4 8 | ./attribute_4 9 | 10 | clean: 11 | rm attribute_4 12 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_1/os/src/process/config.rs: -------------------------------------------------------------------------------- 1 | //! 定义一些进程相关的常量 2 | 3 | /// 每个线程的运行栈大小 512 KB 4 | pub const STACK_SIZE: usize = 0x8_0000; 5 | 6 | /// 共用的内核栈大小 512 KB 7 | pub const KERNEL_STACK_SIZE: usize = 0x8_0000; 8 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_2/os/src/process/config.rs: -------------------------------------------------------------------------------- 1 | //! 定义一些进程相关的常量 2 | 3 | /// 每个线程的运行栈大小 512 KB 4 | pub const STACK_SIZE: usize = 0x8_0000; 5 | 6 | /// 共用的内核栈大小 512 KB 7 | pub const KERNEL_STACK_SIZE: usize = 0x8_0000; 8 | -------------------------------------------------------------------------------- /02-modular-programing/09-csv_challenge/src/core.rs: -------------------------------------------------------------------------------- 1 | // core.rs 2 | 3 | pub mod read; 4 | pub mod write; 5 | use crate::err::Error; 6 | use std::{ 7 | path::PathBuf, 8 | fs::File, 9 | io::{Read, Write} 10 | }; 11 | 12 | -------------------------------------------------------------------------------- /02-modular-programing/10-csv_challenge/src/core.rs: -------------------------------------------------------------------------------- 1 | // core.rs 2 | 3 | pub mod read; 4 | pub mod write; 5 | use crate::err::Error; 6 | use std::{ 7 | path::PathBuf, 8 | fs::File, 9 | io::{Read, Write} 10 | }; 11 | 12 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/functions/functions1.rs: -------------------------------------------------------------------------------- 1 | // functions1.rs 2 | // Make me compile! Execute `rustlings hint functions1` for hints :) 3 | 4 | fn call_me() { 5 | () 6 | } 7 | 8 | fn main() { 9 | call_me(); 10 | } 11 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/variables/README.md: -------------------------------------------------------------------------------- 1 | ### Variables 2 | 3 | Here you'll learn about simple variables. 4 | 5 | #### Book Sections 6 | 7 | - [Variables and Mutability](https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html) 8 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/variables/variables4.rs: -------------------------------------------------------------------------------- 1 | // variables4.rs 2 | // Make me compile! Execute the command `rustlings hint variables4` if you want a hint :) 3 | 4 | fn main() { 5 | let x: i32 = 0; 6 | println!("Number {}", x); 7 | } 8 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab2/os/.gdb_history: -------------------------------------------------------------------------------- 1 | b rust_main 2 | c 3 | n 4 | n 5 | b 54 6 | b 58 7 | c 8 | n 9 | c 10 | n 11 | print vec 12 | print v 13 | n 14 | print i 15 | n 16 | x/10i $pc 17 | c 18 | b rust_main 19 | c 20 | n 21 | n 22 | n 23 | n 24 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter11-ceate/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | rustc --crate-type=lib rary.rs 3 | rustc executable.rs --extern rary=library.rlib 4 | 5 | ls:library.rlib 6 | ls lib* 7 | 8 | run:executable 9 | ./executable 10 | 11 | clean: 12 | rm lib* 13 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/Makefile: -------------------------------------------------------------------------------- 1 | run: 2 | @make -C user build 3 | @make -C os run 4 | 5 | clean: 6 | @make -C user clean 7 | @make -C os clean 8 | 9 | fmt: 10 | @cd os && cargo fmt 11 | @cd os/src/algorithm && cargo fmt 12 | @cd user && cargo fmt -------------------------------------------------------------------------------- /03-rustlilngs-exercises/generics/README.md: -------------------------------------------------------------------------------- 1 | ### Generics 2 | 3 | In this section you'll learn about saving yourself many lines of code with generics! 4 | 5 | ### Book Sections 6 | 7 | - [Generic Data Types](https://doc.rust-lang.org/stable/book/ch10-01-syntax.html) -------------------------------------------------------------------------------- /03-rustlilngs-exercises/threads/README.md: -------------------------------------------------------------------------------- 1 | For this exercise check out the [Dining Philosophers example](https://doc.rust-lang.org/1.4.0/book/dining-philosophers.html) and the chapter [Concurrency](https://doc.rust-lang.org/book/ch16-01-threads.html) of the Rust Book. -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab2/os/.cargo/config: -------------------------------------------------------------------------------- 1 | # 编译的目标平台 2 | [build] 3 | target = "riscv64imac-unknown-none-elf" 4 | 5 | # 使用我们的 linker script 来进行链接 6 | [target.riscv64imac-unknown-none-elf] 7 | rustflags = [ 8 | "-C", "link-arg=-Tsrc/linker.ld", 9 | ] 10 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3/os/.cargo/config: -------------------------------------------------------------------------------- 1 | # 编译的目标平台 2 | [build] 3 | target = "riscv64imac-unknown-none-elf" 4 | 5 | # 使用我们的 linker script 来进行链接 6 | [target.riscv64imac-unknown-none-elf] 7 | rustflags = [ 8 | "-C", "link-arg=-Tsrc/linker.ld", 9 | ] 10 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4/os/.cargo/config: -------------------------------------------------------------------------------- 1 | # 编译的目标平台 2 | [build] 3 | target = "riscv64imac-unknown-none-elf" 4 | 5 | # 使用我们的 linker script 来进行链接 6 | [target.riscv64imac-unknown-none-elf] 7 | rustflags = [ 8 | "-C", "link-arg=-Tsrc/linker.ld", 9 | ] 10 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab5/os/.cargo/config: -------------------------------------------------------------------------------- 1 | # 编译的目标平台 2 | [build] 3 | target = "riscv64imac-unknown-none-elf" 4 | 5 | # 使用我们的 linker script 来进行链接 6 | [target.riscv64imac-unknown-none-elf] 7 | rustflags = [ 8 | "-C", "link-arg=-Tsrc/linker.ld", 9 | ] 10 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/os/.cargo/config: -------------------------------------------------------------------------------- 1 | # 编译的目标平台 2 | [build] 3 | target = "riscv64imac-unknown-none-elf" 4 | 5 | # 使用我们的 linker script 来进行链接 6 | [target.riscv64imac-unknown-none-elf] 7 | rustflags = [ 8 | "-C", "link-arg=-Tsrc/linker.ld", 9 | ] 10 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/user/src/bin/hello_world.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | #[macro_use] 5 | extern crate user_lib; 6 | 7 | #[no_mangle] 8 | pub fn main() -> usize { 9 | println!("Hello world from user mode program!"); 10 | 0 11 | } 12 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter13-attribute/attribute_1.rs: -------------------------------------------------------------------------------- 1 | fn used_function() {} 2 | 3 | #[allow(dead_code)] 4 | fn unused_function() {} 5 | 6 | #[allow(dead_code)] 7 | fn noisy_unused_function() {} 8 | 9 | fn main() { 10 | used_function(); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/generics/generics1.rs: -------------------------------------------------------------------------------- 1 | // This shopping list program isn't compiling! 2 | // Use your knowledge of generics to fix it. 3 | 4 | fn main() { 5 | let mut shopping_list: Vec<&str> = Vec::new(); 6 | shopping_list.push("milk"); 7 | } 8 | 9 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/if/README.md: -------------------------------------------------------------------------------- 1 | ### If 2 | 3 | `if`, the most basic type of control flow, is what you'll learn here. 4 | 5 | #### Book Sections 6 | 7 | - [Control Flow - if expressions](https://doc.rust-lang.org/book/ch03-05-control-flow.html#if-expressions) 8 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/structs/README.md: -------------------------------------------------------------------------------- 1 | ### Structs 2 | 3 | Rust has three struct types: a classic c struct, a tuple struct, and a unit struct. 4 | 5 | #### Book Sections 6 | 7 | - [Structures](https://doc.rust-lang.org/rust-by-example/custom_types/structs.html) 8 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/Makefile: -------------------------------------------------------------------------------- 1 | run: 2 | @make -C user build 3 | @make -C os run 4 | 5 | clean: 6 | @make -C user clean 7 | @make -C os clean 8 | 9 | fmt: 10 | @cd os && cargo fmt 11 | @cd os/src/algorithm && cargo fmt 12 | @cd user && cargo fmt -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/Makefile: -------------------------------------------------------------------------------- 1 | run: 2 | @make -C user build 3 | @make -C os run 4 | 5 | clean: 6 | @make -C user clean 7 | @make -C os clean 8 | 9 | fmt: 10 | @cd os && cargo fmt 11 | @cd os/src/algorithm && cargo fmt 12 | @cd user && cargo fmt -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/Makefile: -------------------------------------------------------------------------------- 1 | run: 2 | @make -C user build 3 | @make -C os run 4 | 5 | clean: 6 | @make -C user clean 7 | @make -C os clean 8 | 9 | fmt: 10 | @cd os && cargo fmt 11 | @cd os/src/algorithm && cargo fmt 12 | @cd user && cargo fmt -------------------------------------------------------------------------------- /04-learn-rust-the-hard-way/ex02.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | // This is a comment 3 | 4 | println!("I could have code like this."); // and comment after is ignored 5 | 6 | // println!("This won't run."); 7 | 8 | println!("This will run."); 9 | } 10 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/os/.cargo/config: -------------------------------------------------------------------------------- 1 | # 编译的目标平台 2 | [build] 3 | target = "riscv64imac-unknown-none-elf" 4 | 5 | # 使用我们的 linker script 来进行链接 6 | [target.riscv64imac-unknown-none-elf] 7 | rustflags = [ 8 | "-C", "link-arg=-Tsrc/linker.ld", 9 | ] 10 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/user/src/bin/hello_world.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | #[macro_use] 5 | extern crate user_lib; 6 | 7 | #[no_mangle] 8 | pub fn main() -> usize { 9 | println!("Hello world from user mode program!"); 10 | 0 11 | } 12 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_1/os/.cargo/config: -------------------------------------------------------------------------------- 1 | # 编译的目标平台 2 | [build] 3 | target = "riscv64imac-unknown-none-elf" 4 | 5 | # 使用我们的 linker script 来进行链接 6 | [target.riscv64imac-unknown-none-elf] 7 | rustflags = [ 8 | "-C", "link-arg=-Tsrc/linker.ld", 9 | ] 10 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_2/os/.cargo/config: -------------------------------------------------------------------------------- 1 | # 编译的目标平台 2 | [build] 3 | target = "riscv64imac-unknown-none-elf" 4 | 5 | # 使用我们的 linker script 来进行链接 6 | [target.riscv64imac-unknown-none-elf] 7 | rustflags = [ 8 | "-C", "link-arg=-Tsrc/linker.ld", 9 | ] 10 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/os/.cargo/config: -------------------------------------------------------------------------------- 1 | # 编译的目标平台 2 | [build] 3 | target = "riscv64imac-unknown-none-elf" 4 | 5 | # 使用我们的 linker script 来进行链接 6 | [target.riscv64imac-unknown-none-elf] 7 | rustflags = [ 8 | "-C", "link-arg=-Tsrc/linker.ld", 9 | ] 10 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/user/src/bin/hello_world.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | #[macro_use] 5 | extern crate user_lib; 6 | 7 | #[no_mangle] 8 | pub fn main() -> usize { 9 | println!("Hello world from user mode program!"); 10 | 0 11 | } 12 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/os/.cargo/config: -------------------------------------------------------------------------------- 1 | # 编译的目标平台 2 | [build] 3 | target = "riscv64imac-unknown-none-elf" 4 | 5 | # 使用我们的 linker script 来进行链接 6 | [target.riscv64imac-unknown-none-elf] 7 | rustflags = [ 8 | "-C", "link-arg=-Tsrc/linker.ld", 9 | ] 10 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/user/src/bin/hello_world.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | #[macro_use] 5 | extern crate user_lib; 6 | 7 | #[no_mangle] 8 | pub fn main() -> usize { 9 | println!("Hello world from user mode program!"); 10 | 0 11 | } 12 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/functions/README.md: -------------------------------------------------------------------------------- 1 | ### Functions 2 | 3 | Here, you'll learn how to write functions and how Rust's compiler can trace things way back. 4 | 5 | #### Book Sections 6 | 7 | - [How Functions Work](https://doc.rust-lang.org/book/ch03-03-how-functions-work.html) 8 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/modules/README.md: -------------------------------------------------------------------------------- 1 | ### Modules 2 | 3 | In this section we'll give you an introduction to Rust's module system. 4 | 5 | #### Book Sections 6 | 7 | - [The Module System](https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html) 8 | -------------------------------------------------------------------------------- /02-modular-programing/10-csv_challenge/input/no_header.csv: -------------------------------------------------------------------------------- 1 | John,Doe,32,Tokyo,Blue,Human 2 | Flip,Helm,12,Canberra,Red,Unknown 3 | Terdos,Bendarian,165,Cracow,Blue,Magic tree 4 | Dominik,Elpos,33,Paris,Purple,Orc 5 | Brad,Doe,42,Dublin,Blue,Human 6 | Ewan,Grath,51,New Delhi,Green,Human 7 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/error_handling/README.md: -------------------------------------------------------------------------------- 1 | For this exercise check out the sections: 2 | - [Error Handling](https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html) 3 | - [Generics](https://doc.rust-lang.org/book/ch10-01-syntax.html) 4 | 5 | of the Rust Book. 6 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/tests/README.md: -------------------------------------------------------------------------------- 1 | ### Tests 2 | 3 | Going out of order from the book to cover tests -- many of the following exercises will ask you to make tests pass! 4 | 5 | #### Book Sections 6 | 7 | - [Writing Tests](https://doc.rust-lang.org/book/ch11-01-writing-tests.html) 8 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter10-mod/mod_5.rs: -------------------------------------------------------------------------------- 1 | mod my; 2 | 3 | fn function() { 4 | println!("called `function()`"); 5 | } 6 | 7 | fn main() { 8 | my::function(); 9 | 10 | function(); 11 | 12 | my::indirect_access(); 13 | 14 | my::nested::function(); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/macros/macros1.rs: -------------------------------------------------------------------------------- 1 | // macros1.rs 2 | // Make me compile! Execute `rustlings hint macros1` for hints :) 3 | 4 | macro_rules! my_macro { 5 | () => { 6 | println!("Check out my macro!"); 7 | }; 8 | } 9 | 10 | fn main() { 11 | my_macro!(); 12 | } 13 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/macros/macros2.rs: -------------------------------------------------------------------------------- 1 | // macros2.rs 2 | // Make me compile! Execute `rustlings hint macros2` for hints :) 3 | 4 | macro_rules! my_macro { 5 | () => { 6 | println!("Check out my macro!"); 7 | }; 8 | } 9 | 10 | fn main() { 11 | my_macro!(); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/variables/variables3.rs: -------------------------------------------------------------------------------- 1 | // variables3.rs 2 | // Make me compile! Execute the command `rustlings hint variables3` if you want a hint :) 3 | 4 | fn main() { 5 | let x = 3; 6 | println!("Number {}", x); 7 | let x = 5; 8 | println!("Number {}", x); 9 | } 10 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter08-flow_control/flow_control_04.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let mut counter = 0; 3 | 4 | let result = loop { 5 | counter += 1; 6 | if counter == 10 { 7 | break counter * 2; 8 | } 9 | }; 10 | 11 | assert_eq!(result, 20); 12 | } 13 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/os/src/algorithm/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! 一些可能用到,而又不好找库的数据结构 2 | //! 3 | //! 以及有多种实现,会留作业的数据结构 4 | #![no_std] 5 | #![feature(drain_filter)] 6 | 7 | extern crate alloc; 8 | 9 | mod allocator; 10 | mod scheduler; 11 | 12 | pub use allocator::*; 13 | pub use scheduler::*; 14 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/os/src/algorithm/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! 一些可能用到,而又不好找库的数据结构 2 | //! 3 | //! 以及有多种实现,会留作业的数据结构 4 | #![no_std] 5 | #![feature(drain_filter)] 6 | 7 | extern crate alloc; 8 | 9 | mod allocator; 10 | mod scheduler; 11 | 12 | pub use allocator::*; 13 | pub use scheduler::*; 14 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/user/src/bin/get_tid.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | #[macro_use] 5 | extern crate user_lib; 6 | 7 | #[no_mangle] 8 | pub fn main() -> usize { 9 | let tid = user_lib::sys_gettid(); 10 | println!("My thread id is {}", tid); 11 | 0 12 | } 13 | 14 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/os/src/algorithm/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! 一些可能用到,而又不好找库的数据结构 2 | //! 3 | //! 以及有多种实现,会留作业的数据结构 4 | #![no_std] 5 | #![feature(drain_filter)] 6 | 7 | extern crate alloc; 8 | 9 | mod allocator; 10 | mod scheduler; 11 | 12 | pub use allocator::*; 13 | pub use scheduler::*; 14 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/user/src/bin/get_tid.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | #[macro_use] 5 | extern crate user_lib; 6 | 7 | #[no_mangle] 8 | pub fn main() -> usize { 9 | let tid = user_lib::sys_gettid(); 10 | println!("My thread id is {}", tid); 11 | 0 12 | } 13 | 14 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter12-cargo/foo/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "foo" 3 | version = "0.1.0" 4 | authors = ["Yukiteru Lee "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter18-error/error_01.rs: -------------------------------------------------------------------------------- 1 | fn give_princess(gift: &str) { 2 | if gift == "snake" { panic!("AAAaaaaa!!!!"); } 3 | // 23333333 4 | println!("I love {}s!!!!!", gift); 5 | } 6 | 7 | fn main() { 8 | give_princess("teddy bear"); 9 | give_princess("snake"); 10 | } 11 | 12 | -------------------------------------------------------------------------------- /02-modular-programing/01-csv-read/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "csv-read" 3 | version = "0.1.0" 4 | authors = ["Yukiteru Lee "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter09-fn/fn_07.rs: -------------------------------------------------------------------------------- 1 | fn call_me(f: F) { 2 | f() 3 | } 4 | 5 | fn function() { 6 | println!("I'm a function!"); 7 | } 8 | 9 | fn main() { 10 | let closure = || println!("I'm a closure!"); 11 | 12 | call_me(closure); 13 | call_me(function); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/clippy/clippy2.rs: -------------------------------------------------------------------------------- 1 | // clippy2.rs 2 | // Make me compile! Execute `rustlings hint clippy2` for hints :) 3 | 4 | fn main() { 5 | let mut res = 42; 6 | let option = Some(12); 7 | if let Some(x) = option { 8 | res += x; 9 | } 10 | println!("{}", res); 11 | } 12 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/functions/functions5.rs: -------------------------------------------------------------------------------- 1 | // functions5.rs 2 | // Make me compile! Execute `rustlings hint functions5` for hints :) 3 | 4 | fn main() { 5 | let answer = square(3); 6 | println!("The answer is {}", answer); 7 | } 8 | 9 | fn square(num: i32) -> i32 { 10 | num * num 11 | } 12 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/functions/functions2.rs: -------------------------------------------------------------------------------- 1 | // functions2.rs 2 | // Make me compile! Execute `rustlings hint functions2` for hints :) 3 | 4 | fn main() { 5 | call_me(3); 6 | } 7 | 8 | fn call_me(num: i32) { 9 | for i in 0..num { 10 | println!("Ring! Call number {}", i + 1); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/functions/functions3.rs: -------------------------------------------------------------------------------- 1 | // functions3.rs 2 | // Make me compile! Execute `rustlings hint functions3` for hints :) 3 | 4 | fn main() { 5 | call_me(3); 6 | } 7 | 8 | fn call_me(num: i32) { 9 | for i in 0..num { 10 | println!("Ring! Call number {}", i + 1); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/modules/modules1.rs: -------------------------------------------------------------------------------- 1 | // modules1.rs 2 | // Make me compile! Execute `rustlings hint modules1` for hints :) 3 | 4 | mod sausage_factory { 5 | pub fn make_sausage() { 6 | println!("sausage!"); 7 | } 8 | } 9 | 10 | fn main() { 11 | sausage_factory::make_sausage(); 12 | } 13 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/variables/variables5.rs: -------------------------------------------------------------------------------- 1 | // variables5.rs 2 | // Make me compile! Execute the command `rustlings hint variables5` if you want a hint :) 3 | 4 | fn main() { 5 | let number = "3"; 6 | println!("Number {}", number); 7 | let number = 3; 8 | println!("Number {}", number); 9 | } 10 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab0/os/src/entry.asm: -------------------------------------------------------------------------------- 1 | .section .text.entry 2 | .global _start 3 | 4 | _start: 5 | la sp, boot_stack_top 6 | call rust_main 7 | 8 | .section .bss.stack 9 | .global boot_stack 10 | boot_stack: 11 | .space 4096 * 16 12 | 13 | .global boot_stack_top 14 | boot_stack_top: 15 | 16 | 17 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab1/os/src/entry.asm: -------------------------------------------------------------------------------- 1 | .section .text.entry 2 | .global _start 3 | 4 | _start: 5 | la sp, boot_stack_top 6 | call rust_main 7 | 8 | .section .bss.stack 9 | .global boot_stack 10 | boot_stack: 11 | .space 4096 * 16 12 | 13 | .global boot_stack_top 14 | boot_stack_top: 15 | 16 | 17 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/user/src/bin/test_fork.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | #[macro_use] 5 | extern crate user_lib; 6 | 7 | #[no_mangle] 8 | pub fn main() -> usize { 9 | let tid = user_lib::sys_clone(); 10 | println!("I'm cloning my self, my id is {}", tid); 11 | 0 12 | } 13 | 14 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/user/src/bin/test_fork.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | #[macro_use] 5 | extern crate user_lib; 6 | 7 | #[no_mangle] 8 | pub fn main() -> usize { 9 | let tid = user_lib::sys_clone(); 10 | println!("I'm cloning my self, my id is {}", tid); 11 | 0 12 | } 13 | 14 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/variables/variables2.rs: -------------------------------------------------------------------------------- 1 | // variables2.rs 2 | // Make me compile! Execute the command `rustlings hint variables2` if you want a hint :) 3 | 4 | fn main() { 5 | let x = 10; 6 | if x == 10 { 7 | println!("Ten!"); 8 | } else { 9 | println!("Not ten!"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/os/src/kernel/process.rs: -------------------------------------------------------------------------------- 1 | //! 进程相关的内核功能 2 | 3 | use super::*; 4 | 5 | pub(super) fn sys_exit(code: usize) -> SyscallResult { 6 | println!( 7 | "Thread {} exit with code {}", 8 | PROCESSOR.get().current_thread().id, 9 | code 10 | ); 11 | SyscallResult::Kill 12 | } 13 | -------------------------------------------------------------------------------- /02-modular-programing/03-use_regex/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "use_regex" 3 | version = "0.1.0" 4 | authors = ["Yukiteru Lee "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | regex = "1.5.5" 11 | 12 | -------------------------------------------------------------------------------- /02-modular-programing/04-use_regex/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "use_regex" 3 | version = "0.1.0" 4 | authors = ["Yukiteru Lee "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | regex = "1.5.5" 11 | 12 | -------------------------------------------------------------------------------- /02-modular-programing/09-csv_challenge/input/challenge.csv: -------------------------------------------------------------------------------- 1 | First Name,Last Name,Age,City,Eyes color,Species 2 | John,Doe,32,Tokyo,Blue,Human 3 | Flip,Helm,12,Canberra,Red,Unknown 4 | Terdos,Bendarian,165,Cracow,Blue,Magic tree 5 | Dominik,Elpos,33,Paris,Purple,Orc 6 | Brad,Doe,42,Dublin,Blue,Human 7 | Ewan,Grath,51,New Delhi,Green,Human 8 | -------------------------------------------------------------------------------- /02-modular-programing/10-csv_challenge/input/challenge.csv: -------------------------------------------------------------------------------- 1 | First Name,Last Name,Age,City,Eyes color,Species 2 | John,Doe,32,Tokyo,Blue,Human 3 | Flip,Helm,12,Canberra,Red,Unknown 4 | Terdos,Bendarian,165,Cracow,Blue,Magic tree 5 | Dominik,Elpos,33,Paris,Purple,Orc 6 | Brad,Doe,42,Dublin,Blue,Human 7 | Ewan,Grath,51,New Delhi,Green,Human 8 | -------------------------------------------------------------------------------- /02-modular-programing/10-csv_challenge/output/test.csv: -------------------------------------------------------------------------------- 1 | First Name,Last Name,Age,City,Eyes color,Species 2 | John,Doe,32,Beijing,Blue,Human 3 | Flip,Helm,12,Beijing,Red,Unknown 4 | Terdos,Bendarian,165,Beijing,Blue,Magic tree 5 | Dominik,Elpos,33,Beijing,Purple,Orc 6 | Brad,Doe,42,Beijing,Blue,Human 7 | Ewan,Grath,51,Beijing,Green,Human 8 | -------------------------------------------------------------------------------- /02-modular-programing/02-csv-read/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "csv-read" 3 | version = "0.1.0" 4 | authors = ["Yukiteru Lee "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | linked-list = "0.0.3" 11 | 12 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/os/src/kernel/process.rs: -------------------------------------------------------------------------------- 1 | //! 进程相关的内核功能 2 | 3 | use super::*; 4 | 5 | pub(super) fn sys_exit(code: usize) -> SyscallResult { 6 | println!( 7 | "thread {} exit with code {}", 8 | PROCESSOR.lock().current_thread().id, 9 | code 10 | ); 11 | SyscallResult::Kill 12 | } 13 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/os/src/kernel/process.rs: -------------------------------------------------------------------------------- 1 | //! 进程相关的内核功能 2 | 3 | use super::*; 4 | 5 | pub(super) fn sys_exit(code: usize) -> SyscallResult { 6 | println!( 7 | "thread {} exit with code {}", 8 | PROCESSOR.lock().current_thread().id, 9 | code 10 | ); 11 | SyscallResult::Kill 12 | } 13 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter15-scope/scope_06.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let mut _mutable_integer = 7i32; 3 | 4 | { 5 | let large_integer = &_mutable_integer; 6 | 7 | // _mutable_integer = 50; 8 | 9 | println!("Immutably borrowed {}", large_integer); 10 | } 11 | 12 | _mutable_integer = 3; 13 | } 14 | 15 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/os/src/kernel/process.rs: -------------------------------------------------------------------------------- 1 | //! 进程相关的内核功能 2 | 3 | use super::*; 4 | 5 | pub(super) fn sys_exit(code: usize) -> SyscallResult { 6 | println!( 7 | "thread {} exit with code {}", 8 | PROCESSOR.lock().current_thread().id, 9 | code 10 | ); 11 | SyscallResult::Kill 12 | } 13 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/tests/tests2.rs: -------------------------------------------------------------------------------- 1 | // tests2.rs 2 | // This test has a problem with it -- make the test compile! Make the test 3 | // pass! Make the test fail! Execute `rustlings hint tests2` for hints :) 4 | 5 | #[cfg(test)] 6 | mod tests { 7 | #[test] 8 | fn you_can_assert_eq() { 9 | assert_eq!(0, 0); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /02-modular-programing/06-static_hashmap/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "static_hashmap" 3 | version = "0.1.0" 4 | authors = ["Yukiteru Lee "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | lazy_static = "1.1.0" 11 | 12 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab1/os/src/interrupt/mod.rs: -------------------------------------------------------------------------------- 1 | //! 中断模块 2 | //! 3 | //! 4 | 5 | mod handler; 6 | mod context; 7 | mod timer; 8 | 9 | /// 初始化中断相关的子模块 10 | /// 11 | /// - [`handler::init`] 12 | /// - [`timer::init`] 13 | pub fn init() { 14 | handler::init(); 15 | timer::init(); 16 | println!("mod interrupt initialized"); 17 | } 18 | -------------------------------------------------------------------------------- /02-modular-programing/07-static_hashmap-2015/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "static_hashmap" 3 | version = "0.1.0" 4 | authors = ["Yukiteru Lee "] 5 | edition = "2015" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | lazy_static = "1.1.0" 11 | 12 | -------------------------------------------------------------------------------- /02-modular-programing/08-static_hashmap-2018/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "static_hashmap" 3 | version = "0.1.0" 4 | authors = ["Yukiteru Lee "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | lazy_static = "1.1.0" 11 | 12 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/user/src/bin/notebook.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | #[macro_use] 5 | extern crate user_lib; 6 | 7 | use user_lib::console::*; 8 | 9 | #[no_mangle] 10 | pub fn main() -> ! { 11 | println!("\x1b[2J"); 12 | loop { 13 | let string = getchars(); 14 | print!("{}", string); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /02-modular-programing/05-use_regex/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "use_regex" 3 | version = "0.1.0" 4 | authors = ["Yukiteru Lee "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | regex = "1.5.5" 11 | lazy_static = "1.1.0" 12 | 13 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter04-variable_bindings/variable_bindings_2.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let _immutable_binding = 1; 3 | let mut mutable_binding = 1; 4 | 5 | println!("Before mutation: {}", mutable_binding); 6 | 7 | mutable_binding += 1; 8 | println!("After mutation: {}", mutable_binding); 9 | 10 | // _immutable_binding += 1; 11 | } 12 | 13 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter14-generics/generics_01.rs: -------------------------------------------------------------------------------- 1 | struct A; 2 | 3 | struct Single(A); 4 | 5 | struct SingleGen(T); 6 | 7 | fn main() { 8 | let _s = Single(A); 9 | 10 | let _char: SingleGen = SingleGen('a'); 11 | 12 | let _t = SingleGen(A); 13 | let _i32 = SingleGen(6); 14 | let _char = SingleGen('a'); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter15-scope/scope_12.rs: -------------------------------------------------------------------------------- 1 | struct Owner(i32); 2 | 3 | impl Owner { 4 | fn add_one<'a>(&'a mut self) { self.0 += 1; } 5 | fn print<'a>(&'a self) { 6 | println!("`print`: {}", self.0); 7 | } 8 | } 9 | 10 | fn main() { 11 | let mut owner = Owner(18); 12 | 13 | owner.add_one(); 14 | owner.print(); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /04-learn-rust-the-hard-way/ex01.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("Hello, World!"); 3 | // println!("Hello Again!"); 4 | println!("I like typing this."); 5 | println!("This is fun."); 6 | println!("Yay! Printing."); 7 | println!("I'd much rather you 'not'."); 8 | println!("I \"said\" do not touch this."); 9 | println!("TEST"); 10 | } 11 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab2/os/src/interrupt/mod.rs: -------------------------------------------------------------------------------- 1 | //! 中断模块 2 | 3 | mod context; 4 | mod handler; 5 | mod timer; 6 | 7 | pub use context::Context; 8 | 9 | /// 初始化中断相关的子模块 10 | /// 11 | /// - [`handler::init`] 12 | /// - [`timer::init`] 13 | pub fn init() { 14 | handler::init(); 15 | timer::init(); 16 | println!("mod interrupt initialized"); 17 | } 18 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3/os/src/interrupt/mod.rs: -------------------------------------------------------------------------------- 1 | //! 中断模块 2 | 3 | mod context; 4 | mod handler; 5 | mod timer; 6 | 7 | pub use context::Context; 8 | 9 | /// 初始化中断相关的子模块 10 | /// 11 | /// - [`handler::init`] 12 | /// - [`timer::init`] 13 | pub fn init() { 14 | handler::init(); 15 | timer::init(); 16 | println!("mod interrupt initialized"); 17 | } 18 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/user/src/bin/notebook.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | #[macro_use] 5 | extern crate user_lib; 6 | 7 | use user_lib::console::*; 8 | 9 | #[no_mangle] 10 | pub fn main() -> ! { 11 | println!("\x1b[2J"); 12 | loop { 13 | let string = getchars(); 14 | print!("{}", string); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/user/src/bin/notebook.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | #[macro_use] 5 | extern crate user_lib; 6 | 7 | use user_lib::console::*; 8 | 9 | #[no_mangle] 10 | pub fn main() -> ! { 11 | println!("\x1b[2J"); 12 | loop { 13 | let string = getchars(); 14 | print!("{}", string); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/user/src/bin/notebook.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | #[macro_use] 5 | extern crate user_lib; 6 | 7 | use user_lib::console::*; 8 | 9 | #[no_mangle] 10 | pub fn main() -> ! { 11 | println!("\x1b[2J"); 12 | loop { 13 | let string = getchars(); 14 | print!("{}", string); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /02-modular-programing/09-csv_challenge/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "csv_challenge" 3 | version = "0.1.0" 4 | authors = ["Yukiteru Lee "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | structopt = "0.2" 11 | structopt-derive = "0.2" 12 | 13 | -------------------------------------------------------------------------------- /02-modular-programing/10-csv_challenge/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "csv_challenge" 3 | version = "0.1.0" 4 | authors = ["Yukiteru Lee "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | structopt = "0.2" 11 | structopt-derive = "0.2" 12 | 13 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab2_practice/segment_tree_allocator_array/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "segment_tree_allocator" 3 | version = "0.1.0" 4 | authors = ["Yukiteru Lee "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | spin = "0.5.2" 11 | 12 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/user/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "user_lib" 3 | version = "0.1.0" 4 | authors = ["涂轶翔 ", "赵成钢 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | buddy_system_allocator = "0.3.9" -------------------------------------------------------------------------------- /01-rust-by-example/chapter11-ceate/rary.rs: -------------------------------------------------------------------------------- 1 | pub fn public_function() { 2 | println!("called rary's `public_function()`"); 3 | } 4 | 5 | fn private_function() { 6 | println!("called rary's `private_function()`"); 7 | } 8 | 9 | pub fn indirect_access() { 10 | print!("called rary's `indirect_access()`, that\n> "); 11 | 12 | private_function(); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab2_practice/segment_tree_allocator_pointer/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "segment_tree_allocator" 3 | version = "0.1.0" 4 | authors = ["Yukiteru Lee "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | spin = "0.5.2" 11 | 12 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab2/os/src/algorithm/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "algorithm" 3 | version = "0.1.0" 4 | authors = ["涂轶翔 ", "赵成钢 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | bit_field = "0.10.0" 11 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3/os/src/algorithm/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "algorithm" 3 | version = "0.1.0" 4 | authors = ["涂轶翔 ", "赵成钢 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | bit_field = "0.10.0" 11 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/user/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "user_lib" 3 | version = "0.1.0" 4 | authors = ["涂轶翔 ", "赵成钢 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | buddy_system_allocator = "0.3.9" -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4/os/src/algorithm/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "algorithm" 3 | version = "0.1.0" 4 | authors = ["涂轶翔 ", "赵成钢 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | bit_field = "0.10.0" 11 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4/os/src/algorithm/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! 一些可能用到,而又不好找库的数据结构 2 | //! 3 | //! 以及有多种实现,会留作业的数据结构 4 | #![no_std] 5 | #![feature(drain_filter)] 6 | 7 | extern crate alloc; 8 | 9 | mod allocator; 10 | mod scheduler; 11 | mod unsafe_wrapper; 12 | 13 | pub use allocator::*; 14 | pub use scheduler::*; 15 | pub use unsafe_wrapper::{StaticUnsafeWrapper, UnsafeWrapper}; 16 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4/os/src/interrupt/mod.rs: -------------------------------------------------------------------------------- 1 | //! 中断模块 2 | //! 3 | //! 4 | 5 | mod context; 6 | mod handler; 7 | mod timer; 8 | 9 | pub use context::Context; 10 | 11 | /// 初始化中断相关的子模块 12 | /// 13 | /// - [`handler::init`] 14 | /// - [`timer::init`] 15 | pub fn init() { 16 | handler::init(); 17 | timer::init(); 18 | println!("mod interrupt initialized"); 19 | } 20 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab5/os/src/algorithm/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "algorithm" 3 | version = "0.1.0" 4 | authors = ["涂轶翔 ", "赵成钢 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | bit_field = "0.10.0" 11 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab5/os/src/algorithm/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! 一些可能用到,而又不好找库的数据结构 2 | //! 3 | //! 以及有多种实现,会留作业的数据结构 4 | #![no_std] 5 | #![feature(drain_filter)] 6 | 7 | extern crate alloc; 8 | 9 | mod allocator; 10 | mod scheduler; 11 | mod unsafe_wrapper; 12 | 13 | pub use allocator::*; 14 | pub use scheduler::*; 15 | pub use unsafe_wrapper::{StaticUnsafeWrapper, UnsafeWrapper}; 16 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab5/os/src/interrupt/mod.rs: -------------------------------------------------------------------------------- 1 | //! 中断模块 2 | //! 3 | //! 4 | 5 | mod context; 6 | mod handler; 7 | mod timer; 8 | 9 | pub use context::Context; 10 | 11 | /// 初始化中断相关的子模块 12 | /// 13 | /// - [`handler::init`] 14 | /// - [`timer::init`] 15 | pub fn init() { 16 | handler::init(); 17 | timer::init(); 18 | println!("mod interrupt initialized"); 19 | } 20 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/os/src/algorithm/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "algorithm" 3 | version = "0.1.0" 4 | authors = ["涂轶翔 ", "赵成钢 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | bit_field = "0.10.0" 11 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/os/src/algorithm/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! 一些可能用到,而又不好找库的数据结构 2 | //! 3 | //! 以及有多种实现,会留作业的数据结构 4 | #![no_std] 5 | #![feature(drain_filter)] 6 | 7 | extern crate alloc; 8 | 9 | mod allocator; 10 | mod scheduler; 11 | mod unsafe_wrapper; 12 | 13 | pub use allocator::*; 14 | pub use scheduler::*; 15 | pub use unsafe_wrapper::{StaticUnsafeWrapper, UnsafeWrapper}; 16 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/user/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "user_lib" 3 | version = "0.1.0" 4 | authors = ["涂轶翔 ", "赵成钢 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | buddy_system_allocator = "0.3.9" -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/user/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "user_lib" 3 | version = "0.1.0" 4 | authors = ["涂轶翔 ", "赵成钢 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | buddy_system_allocator = "0.3.9" -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab0/os/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "os" 3 | version = "0.1.0" 4 | authors = ["Yukiteru Lee "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | 11 | [profile.dev] 12 | panic = "abort" 13 | 14 | [profile.release] 15 | panic = "abort" 16 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/os/src/algorithm/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "algorithm" 3 | version = "0.1.0" 4 | authors = ["涂轶翔 ", "赵成钢 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | bit_field = "0.10.0" 11 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/os/src/algorithm/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "algorithm" 3 | version = "0.1.0" 4 | authors = ["涂轶翔 ", "赵成钢 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | bit_field = "0.10.0" 11 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter17-macro/macro_4.rs: -------------------------------------------------------------------------------- 1 | macro_rules! find_min { 2 | ($x:expr) => ($x); 3 | ($x:expr, $($y:expr),+) => ( 4 | std::cmp::min($x, find_min!($($y),+)) 5 | ) 6 | } 7 | 8 | fn main() { 9 | println!("{}", find_min!(1u32)); 10 | println!("{}", find_min!(1u32 + 2 , 2u32)); 11 | println!("{}", find_min!(5u32, 2u32 * 3, 4u32)); 12 | } 13 | 14 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/strings/README.md: -------------------------------------------------------------------------------- 1 | ### Strings 2 | 3 | Rust has two string types, a string slice (`&str`) and an owned string (`String`). 4 | We're not going to dictate when you should use which one, but we'll show you how 5 | to identify and create them, as well as use them. 6 | 7 | #### Book Sections 8 | 9 | - [Strings](https://doc.rust-lang.org/book/ch08-02-strings.html) 10 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/os/src/interrupt/mod.rs: -------------------------------------------------------------------------------- 1 | //! 中断模块 2 | //! 3 | //! 4 | 5 | mod context; 6 | mod handler; 7 | mod timer; 8 | 9 | pub use context::Context; 10 | 11 | /// 初始化中断相关的子模块 12 | /// 13 | /// - [`handler::init`] 14 | /// - [`timer::init`] 15 | pub fn init() { 16 | handler::init(); 17 | timer::init(); 18 | println!("mod interrupt initialized"); 19 | } 20 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_1/os/src/algorithm/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "algorithm" 3 | version = "0.1.0" 4 | authors = ["涂轶翔 ", "赵成钢 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | bit_field = "0.10.0" 11 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_1/os/src/algorithm/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! 一些可能用到,而又不好找库的数据结构 2 | //! 3 | //! 以及有多种实现,会留作业的数据结构 4 | #![no_std] 5 | #![feature(drain_filter)] 6 | 7 | extern crate alloc; 8 | 9 | mod allocator; 10 | mod scheduler; 11 | mod unsafe_wrapper; 12 | 13 | pub use allocator::*; 14 | pub use scheduler::*; 15 | pub use unsafe_wrapper::{StaticUnsafeWrapper, UnsafeWrapper}; 16 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_2/os/src/algorithm/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "algorithm" 3 | version = "0.1.0" 4 | authors = ["涂轶翔 ", "赵成钢 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | bit_field = "0.10.0" 11 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_2/os/src/algorithm/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! 一些可能用到,而又不好找库的数据结构 2 | //! 3 | //! 以及有多种实现,会留作业的数据结构 4 | #![no_std] 5 | #![feature(drain_filter)] 6 | 7 | extern crate alloc; 8 | 9 | mod allocator; 10 | mod scheduler; 11 | mod unsafe_wrapper; 12 | 13 | pub use allocator::*; 14 | pub use scheduler::*; 15 | pub use unsafe_wrapper::{StaticUnsafeWrapper, UnsafeWrapper}; 16 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/os/src/algorithm/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "algorithm" 3 | version = "0.1.0" 4 | authors = ["涂轶翔 ", "赵成钢 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | bit_field = "0.10.0" 11 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/os/src/interrupt/mod.rs: -------------------------------------------------------------------------------- 1 | //! 中断模块 2 | //! 3 | //! 4 | 5 | mod context; 6 | mod handler; 7 | mod timer; 8 | 9 | pub use context::Context; 10 | 11 | /// 初始化中断相关的子模块 12 | /// 13 | /// - [`handler::init`] 14 | /// - [`timer::init`] 15 | pub fn init() { 16 | handler::init(); 17 | timer::init(); 18 | println!("mod interrupt initialized"); 19 | } 20 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/os/src/interrupt/mod.rs: -------------------------------------------------------------------------------- 1 | //! 中断模块 2 | //! 3 | //! 4 | 5 | mod context; 6 | mod handler; 7 | mod timer; 8 | 9 | pub use context::Context; 10 | 11 | /// 初始化中断相关的子模块 12 | /// 13 | /// - [`handler::init`] 14 | /// - [`timer::init`] 15 | pub fn init() { 16 | handler::init(); 17 | timer::init(); 18 | println!("mod interrupt initialized"); 19 | } 20 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/primitive_types/primitive_types5.rs: -------------------------------------------------------------------------------- 1 | // primitive_types5.rs 2 | // Destructure the `cat` tuple so that the println will work. 3 | // Execute `rustlings hint primitive_types5` for hints! 4 | 5 | fn main() { 6 | let cat = ("Furry McFurson", 3.5); 7 | let (name, age) /* your pattern here */ = cat; 8 | 9 | println!("{} is {} years old.", name, age); 10 | } 11 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/primitive_types/primitive_types6.rs: -------------------------------------------------------------------------------- 1 | // primitive_types6.rs 2 | // Use a tuple index to access the second element of `numbers`. 3 | // You can put this right into the `println!` where the ??? is. 4 | // Execute `rustlings hint primitive_types6` for hints! 5 | 6 | fn main() { 7 | let numbers = (1, 2, 3); 8 | println!("The second number is {}", numbers.1); 9 | } 10 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_1/os/src/interrupt/mod.rs: -------------------------------------------------------------------------------- 1 | //! 中断模块 2 | //! 3 | //! 4 | 5 | mod context; 6 | mod handler; 7 | mod timer; 8 | 9 | pub use context::Context; 10 | 11 | /// 初始化中断相关的子模块 12 | /// 13 | /// - [`handler::init`] 14 | /// - [`timer::init`] 15 | pub fn init() { 16 | handler::init(); 17 | timer::init(); 18 | println!("mod interrupt initialized"); 19 | } 20 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_2/os/src/interrupt/mod.rs: -------------------------------------------------------------------------------- 1 | //! 中断模块 2 | //! 3 | //! 4 | 5 | mod context; 6 | mod handler; 7 | mod timer; 8 | 9 | pub use context::Context; 10 | 11 | /// 初始化中断相关的子模块 12 | /// 13 | /// - [`handler::init`] 14 | /// - [`timer::init`] 15 | pub fn init() { 16 | handler::init(); 17 | timer::init(); 18 | println!("mod interrupt initialized"); 19 | } 20 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter05-types/types_4.rs: -------------------------------------------------------------------------------- 1 | type NanoSecond = u64; 2 | type Inch = u64; 3 | 4 | type u64_t = u64; 5 | 6 | fn main() { 7 | let nanoseconds: NanoSecond = 5 as u64_t; 8 | let inches: Inch = 2 as u64_t; 9 | 10 | println!("{} nanoseconds + {} inches = {} unit?", 11 | nanoseconds, 12 | inches, 13 | nanoseconds + inches); 14 | } 15 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter08-flow_control/flow_control_03.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | 'outer: loop { 3 | println!("Entered the outer loop"); 4 | 5 | 'inner: loop { 6 | println!("Entered the inner loop"); 7 | 8 | break 'outer; 9 | } 10 | println!("This point will never be reached"); 11 | } 12 | println!("Exited the outer loop"); 13 | } 14 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter08-flow_control/flow_control_08.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let pair = (0, -2); 3 | 4 | println!("Tell me about {:?}", pair); 5 | match pair { 6 | (0, y) => println!("First is `0` and `y` is `{:?}`", y), 7 | (x, 0) => println!("`x` is `{:?}` and last is `0`", x), 8 | _ => println!("It doesn't matter what they are"), 9 | } 10 | } 11 | 12 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter15-scope/scope_10.rs: -------------------------------------------------------------------------------- 1 | fn print_refs<'a, 'b>(x: &'a i32, y: &'b i32) { 2 | println!("x is {} and y is {}", x, y); 3 | } 4 | 5 | fn failed_borrow<'a>() { 6 | let _x = 12; 7 | 8 | // let y: &'a i32 = &_x; 9 | } 10 | 11 | fn main() { 12 | let (four, nine) = (4, 9); 13 | 14 | print_refs(&four, &nine); 15 | 16 | failed_borrow(); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/primitive_types/primitive_types4.rs: -------------------------------------------------------------------------------- 1 | // primitive_types4.rs 2 | // Get a slice out of Array a where the ??? is so that the test passes. 3 | // Execute `rustlings hint primitive_types4` for hints!! 4 | 5 | #[test] 6 | fn slice_out_of_array() { 7 | let a = [1, 2, 3, 4, 5]; 8 | 9 | let nice_slice = &a[1..=3]; 10 | 11 | assert_eq!([2, 3, 4], nice_slice) 12 | } 13 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/strings/strings1.rs: -------------------------------------------------------------------------------- 1 | // strings1.rs 2 | // Make me compile without changing the function signature! 3 | // Execute `rustlings hint strings1` for hints ;) 4 | 5 | fn main() { 6 | let answer = current_favorite_color(); 7 | println!("My current favorite color is {}", answer); 8 | } 9 | 10 | fn current_favorite_color() -> String { 11 | String::from("blue") 12 | } 13 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter10-mod/my/mod.rs: -------------------------------------------------------------------------------- 1 | mod inaccessible; 2 | pub mod nested; 3 | 4 | pub fn function() { 5 | println!("called `my::function()`"); 6 | } 7 | 8 | fn private_function() { 9 | println!("called `my::private_function()`"); 10 | } 11 | 12 | pub fn indirect_access() { 13 | print!("called `my::indirect_access()`, that\n> "); 14 | 15 | private_function(); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter15-scope/scope_14.rs: -------------------------------------------------------------------------------- 1 | #[derive(Debug)] 2 | struct Borrowed<'a> { 3 | x: &'a i32, 4 | } 5 | 6 | // why??? 7 | impl<'a> Default for Borrowed<'a> { 8 | fn default() -> Self { 9 | Self { 10 | x: &10, 11 | } 12 | } 13 | } 14 | 15 | fn main() { 16 | let b: Borrowed = Default::default(); 17 | println!("b is {:?}", b); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter17-macro/macro_6.rs: -------------------------------------------------------------------------------- 1 | macro_rules! calculate { 2 | (eval $e:expr) => {{ 3 | { 4 | let val: usize = $e; 5 | println!("{} = {}", stringify!{$e}, val); 6 | } 7 | }}; 8 | } 9 | 10 | fn main() { 11 | calculate! { 12 | eval 1 + 2 13 | } 14 | 15 | calculate! { 16 | eval (1 + 2) * (3 / 4) 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter19-std/std_7.rs: -------------------------------------------------------------------------------- 1 | fn division(dividend: i32, divisor: i32) -> i32 { 2 | if divisor == 0 { 3 | // 除以 0 会引发 panic 4 | panic!("division by zero"); 5 | } else { 6 | dividend / divisor 7 | } 8 | } 9 | 10 | fn main() { 11 | let _x = Box::new(0i32); 12 | 13 | division(3, 0); 14 | 15 | println!("This point won't be reached!"); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/os/src/kernel/mod.rs: -------------------------------------------------------------------------------- 1 | //! 为进程提供系统调用等内核功能 2 | 3 | mod condvar; 4 | mod fs; 5 | mod process; 6 | mod syscall; 7 | 8 | use crate::interrupt::*; 9 | use crate::process::*; 10 | use alloc::sync::Arc; 11 | pub(self) use fs::*; 12 | pub(self) use process::*; 13 | use spin::Mutex; 14 | pub(self) use syscall::*; 15 | 16 | pub use condvar::Condvar; 17 | pub use syscall::syscall_handler; 18 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter14-generics/generics_04.rs: -------------------------------------------------------------------------------- 1 | struct Empty; 2 | struct Null; 3 | 4 | trait DoubleDrop { 5 | fn double_drop(self, _: T); 6 | } 7 | 8 | impl DoubleDrop for U { 9 | fn double_drop(self, _: T) {} 10 | } 11 | 12 | fn main() { 13 | let empty = Empty; 14 | let null = Null; 15 | 16 | empty.double_drop(null); 17 | 18 | // empty; 19 | // null; 20 | } 21 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/macros/macros4.rs: -------------------------------------------------------------------------------- 1 | // macros4.rs 2 | // Make me compile! Execute `rustlings hint macros4` for hints :) 3 | 4 | macro_rules! my_macro { 5 | () => { 6 | println!("Check out my macro!"); 7 | }; 8 | ($val:expr) => { 9 | println!("Look at this other macro: {}", $val); 10 | }; 11 | } 12 | 13 | fn main() { 14 | my_macro!(); 15 | my_macro!(7777); 16 | } 17 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/option/README.md: -------------------------------------------------------------------------------- 1 | ### Option 2 | 3 | #### Book Sections 4 | 5 | To learn about Option, check out these links: 6 | 7 | - [Option Enum Format](https://doc.rust-lang.org/stable/book/ch10-01-syntax.html#in-enum-definitions) 8 | - [Option Module Documentation](https://doc.rust-lang.org/std/option/) 9 | - [Option Enum Documentation](https://doc.rust-lang.org/std/option/enum.Option.html) 10 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/os/src/kernel/mod.rs: -------------------------------------------------------------------------------- 1 | //! 为进程提供系统调用等内核功能 2 | 3 | mod condvar; 4 | mod fs; 5 | mod process; 6 | mod syscall; 7 | 8 | use crate::interrupt::*; 9 | use crate::process::*; 10 | use alloc::sync::Arc; 11 | pub(self) use fs::*; 12 | pub(self) use process::*; 13 | use spin::Mutex; 14 | pub(self) use syscall::*; 15 | 16 | pub use condvar::Condvar; 17 | pub use syscall::syscall_handler; 18 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter08-flow_control/flow_control_14.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let mut optional = Some(0); 3 | 4 | while let Some(i) = optional { 5 | if i > 9 { 6 | println!("Greater than 9, quit!"); 7 | optional = None; 8 | } else { 9 | println!("`i` is `{:?}`. Try again.", i); 10 | optional = Some(i + 1); 11 | } 12 | } 13 | } 14 | 15 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/clippy/clippy1.rs: -------------------------------------------------------------------------------- 1 | // clippy1.rs 2 | // The Clippy tool is a collection of lints to analyze your code 3 | // so you can catch common mistakes and improve your Rust code. 4 | // 5 | // Execute `rustlings hint clippy1` for hints :) 6 | 7 | fn main() { 8 | let x = 1.2331f64; 9 | let y = 1.2332f64; 10 | if (y - x).abs() > f64::EPSILON { 11 | println!("Success!"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/primitive_types/README.md: -------------------------------------------------------------------------------- 1 | ### Primitive Types 2 | 3 | Rust has a couple of basic types that are directly implemented into the 4 | compiler. In this section, we'll go through the most important ones. 5 | 6 | #### Book Sections 7 | 8 | - [Data Types](https://doc.rust-lang.org/stable/book/ch03-02-data-types.html) 9 | - [The Slice Type](https://doc.rust-lang.org/stable/book/ch04-03-slices.html) 10 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/macros/macros3.rs: -------------------------------------------------------------------------------- 1 | // macros3.rs 2 | // Make me compile, without taking the macro out of the module! 3 | // Execute `rustlings hint macros3` for hints :) 4 | 5 | #[macro_use] // I cant understand it 6 | mod macros { 7 | macro_rules! my_macro { 8 | () => { 9 | println!("Check out my macro!"); 10 | }; 11 | } 12 | } 13 | 14 | fn main() { 15 | my_macro!(); 16 | } 17 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter15-scope/scope_03.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let immutable_box = Box::new(5u32); 3 | 4 | println!("immutable_box contains {}", immutable_box); 5 | 6 | // *immutable_box = 4; 7 | 8 | let mut mutable_box = immutable_box; 9 | 10 | println!("mutable_box contains {}", mutable_box); 11 | 12 | *mutable_box = 4; 13 | 14 | println!("mutable_box now contains {}", mutable_box); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/README.md: -------------------------------------------------------------------------------- 1 | # rCore 实验记录 2 | 3 | * [Lab 0](lab0.md) 4 | * [Lab 1](lab1.md) 5 | * [Lab 2](lab2.md) 6 | * [Lab 3](lab3.md) 7 | * [Lab 4](lab4.md) 8 | * [Lab 5](lab5.md) 9 | * [Lab 6](lab6.md) 10 | 11 | ## rCore 实验 12 | 13 | * [实验一](lab1_practice.md) 14 | * [实验二](lab2_practice.md) 15 | * [实验三](lab3_practice.md) 16 | * [实验四(上)](lab4_practice_1.md) 17 | * [实验四(下)](lab4_practice_2.md) 18 | * [实验六](lab6_practice.md) 19 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter14-generics/generics_08.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::Debug; 2 | 3 | trait PrintInOption { 4 | fn print_in_option(self); 5 | } 6 | 7 | impl PrintInOption for T where Option: Debug { 8 | // why Option ? 9 | fn print_in_option(self) { 10 | println!("{:?}", Some(self)); 11 | } 12 | } 13 | 14 | fn main() { 15 | let vec = vec![1, 2, 3]; 16 | 17 | vec.print_in_option(); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter04-variable_bindings/variable_bindings_4.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let a_binding; 3 | 4 | { 5 | let x = 2; 6 | a_binding = x * x; 7 | } 8 | 9 | println!("a binding: {}", a_binding); 10 | 11 | let another_binding; 12 | // println!("another binding: {}", another_binding); 13 | 14 | another_binding = 1; 15 | println!("another binding: {}", another_binding); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter08-flow_control/flow_control_12.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let pair = (2, -2); 3 | 4 | println!("Tell me about {:?}", pair); 5 | match pair { 6 | (x, y) if x == y => println!("These are twins"), 7 | (x, y) if x + y == 0 => println!("Antimatter, kaboom!"), 8 | (x, _) if x % 2 == 1 => println!("The first one is odd"), 9 | _ => println!("No correlation..."), 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /02-modular-programing/09-csv_challenge/src/err.rs: -------------------------------------------------------------------------------- 1 | use std::io; 2 | 3 | #[derive(Debug)] 4 | pub enum Error { 5 | Io(io::Error), 6 | Program(&'static str), 7 | } 8 | 9 | impl From for Error { 10 | fn from(e: io::Error) -> Error { 11 | Error::Io(e) 12 | } 13 | } 14 | 15 | impl From<&'static str> for Error { 16 | fn from(e: &'static str) -> Error { 17 | Error::Program(e) 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /02-modular-programing/10-csv_challenge/src/err.rs: -------------------------------------------------------------------------------- 1 | use std::io; 2 | 3 | #[derive(Debug)] 4 | pub enum Error { 5 | Io(io::Error), 6 | Program(&'static str), 7 | } 8 | 9 | impl From for Error { 10 | fn from(e: io::Error) -> Error { 11 | Error::Io(e) 12 | } 13 | } 14 | 15 | impl From<&'static str> for Error { 16 | fn from(e: &'static str) -> Error { 17 | Error::Program(e) 18 | } 19 | } 20 | 21 | -------------------------------------------------------------------------------- /04-learn-rust-the-hard-way/ex10.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let tabby_cat = "\tI'm tabbed in."; 3 | let persian_cat = "I'm split\non a line."; 4 | let backslash_cat = "I'm \\ a \\ cat."; 5 | 6 | let fat_cat = "I'll do a list:\n\t* Cat food\n\t* FIshes\n\t* Catnip\n\t* Grass"; 7 | 8 | println!("{}", tabby_cat); 9 | println!("{}", persian_cat); 10 | println!("{}", backslash_cat); 11 | println!("{}", fat_cat); 12 | } 13 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter08-flow_control/flow_control_05.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let mut n = 1; 3 | 4 | while n < 101 { 5 | if n % 15 == 0 { 6 | println!("fizzbuzz"); 7 | } else if n % 3 == 0 { 8 | println!("fizz"); 9 | } else if n % 5 == 0 { 10 | println!("buzz"); 11 | } else { 12 | println!("{}", n); 13 | } 14 | n += 1; 15 | } 16 | } 17 | 18 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter13-attribute/attribute_2.rs: -------------------------------------------------------------------------------- 1 | #![crate_type = "lib"] 2 | #![crate_name = "rary"] 3 | 4 | pub fn public_function() { 5 | println!("called rary's `public_function()`"); 6 | } 7 | 8 | fn private_function() { 9 | println!("called rary's `private_function()`"); 10 | } 11 | 12 | pub fn indirect_access() { 13 | print!("called rary's `indirect_access()`, that\n> "); 14 | 15 | private_function(); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab5/os/src/drivers/mod.rs: -------------------------------------------------------------------------------- 1 | //! 驱动模块 2 | //! 3 | //! 负责驱动管理 4 | 5 | use crate::memory::{PhysicalAddress, VirtualAddress}; 6 | 7 | pub mod block; 8 | pub mod bus; 9 | pub mod device_tree; 10 | pub mod driver; 11 | 12 | /// 从设备树的物理地址来获取全部设备信息并初始化 13 | pub fn init(dtb_pa: PhysicalAddress) { 14 | let dtb_va = VirtualAddress::from(dtb_pa); 15 | device_tree::init(dtb_va); 16 | println!("mod driver initialized") 17 | } 18 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/os/src/drivers/mod.rs: -------------------------------------------------------------------------------- 1 | //! 驱动模块 2 | //! 3 | //! 负责驱动管理 4 | 5 | use crate::memory::{PhysicalAddress, VirtualAddress}; 6 | 7 | pub mod block; 8 | pub mod bus; 9 | pub mod device_tree; 10 | pub mod driver; 11 | 12 | /// 从设备树的物理地址来获取全部设备信息并初始化 13 | pub fn init(dtb_pa: PhysicalAddress) { 14 | let dtb_va = VirtualAddress::from(dtb_pa); 15 | device_tree::init(dtb_va); 16 | println!("mod driver initialized") 17 | } 18 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter03-custom_types/custom_types_6.rs: -------------------------------------------------------------------------------- 1 | static LANGUAGE: &'static str = "Rust"; 2 | const THRESHOLD: i32 = 10; 3 | 4 | fn is_big(n: i32) -> bool { 5 | n > THRESHOLD 6 | } 7 | 8 | fn main() { 9 | let n = 16; 10 | 11 | println!("This is {}", LANGUAGE); 12 | println!("The threshold is {}", THRESHOLD); 13 | println!("{} is {}", n, if is_big(n) { "big" } else { "small" }); 14 | 15 | //THRESHOLD = 5; 16 | } 17 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter09-fn/fn_08.rs: -------------------------------------------------------------------------------- 1 | fn create_fn() -> impl Fn() { 2 | let text = "Fn".to_owned(); 3 | move || println!("This is a: {}", text) 4 | } 5 | 6 | fn create_fnmut() -> impl FnMut() { 7 | let text = "FnMut".to_owned(); 8 | move || println!("This ia a: {}", text) 9 | } 10 | 11 | fn main() { 12 | let fn_plain = create_fn(); 13 | let mut fn_mut = create_fnmut(); 14 | 15 | fn_plain(); 16 | fn_mut(); 17 | } 18 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter14-generics/generics_02.rs: -------------------------------------------------------------------------------- 1 | struct A; 2 | struct S(A); 3 | struct SGen(T); 4 | 5 | fn reg_fn(_s: S) {} 6 | 7 | fn gen_spec_t(_s: SGen) {} 8 | 9 | fn gen_spec_i32(_s: SGen) {} 10 | 11 | fn generic(_s: SGen) {} 12 | 13 | fn main() { 14 | reg_fn(S(A)); 15 | gen_spec_t(SGen(A)); 16 | gen_spec_i32(SGen(6)); 17 | 18 | generic::(SGen('a')); 19 | 20 | generic(SGen('c')); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/primitive_types/primitive_types3.rs: -------------------------------------------------------------------------------- 1 | // primitive_types3.rs 2 | // Create an array with at least 100 elements in it where the ??? is. 3 | // Execute `rustlings hint primitive_types3` for hints! 4 | 5 | fn main() { 6 | let a = (0..105); 7 | 8 | if a.len() >= 100 { 9 | println!("Wow, that's a big array!"); 10 | } else { 11 | println!("Meh, I eat arrays like that for breakfast."); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter02-primitives/primitive_1.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let logical: bool = true; 3 | 4 | let a_float: f64 = 1.0; 5 | let an_integer = 5i32; 6 | 7 | let default_float = 3.0; 8 | let default_integer = 7; 9 | 10 | let mut inferred_type = 12; 11 | inferred_type = 4294967296i64; 12 | 13 | let mut mutable = 12; 14 | mutable = 21; 15 | 16 | // mutable = true; 17 | 18 | let mutable = true; 19 | } 20 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/os/src/drivers/mod.rs: -------------------------------------------------------------------------------- 1 | //! 驱动模块 2 | //! 3 | //! 负责驱动管理 4 | 5 | use crate::memory::{PhysicalAddress, VirtualAddress}; 6 | 7 | pub mod block; 8 | pub mod bus; 9 | pub mod device_tree; 10 | pub mod driver; 11 | 12 | /// 从设备树的物理地址来获取全部设备信息并初始化 13 | pub fn init(dtb_pa: PhysicalAddress) { 14 | let dtb_va = VirtualAddress::from(dtb_pa); 15 | device_tree::init(dtb_va); 16 | println!("mod driver initialized") 17 | } 18 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/os/src/drivers/mod.rs: -------------------------------------------------------------------------------- 1 | //! 驱动模块 2 | //! 3 | //! 负责驱动管理 4 | 5 | use crate::memory::{PhysicalAddress, VirtualAddress}; 6 | 7 | pub mod block; 8 | pub mod bus; 9 | pub mod device_tree; 10 | pub mod driver; 11 | 12 | /// 从设备树的物理地址来获取全部设备信息并初始化 13 | pub fn init(dtb_pa: PhysicalAddress) { 14 | let dtb_va = VirtualAddress::from(dtb_pa); 15 | device_tree::init(dtb_va); 16 | println!("mod driver initialized") 17 | } 18 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/os/src/drivers/mod.rs: -------------------------------------------------------------------------------- 1 | //! 驱动模块 2 | //! 3 | //! 负责驱动管理 4 | 5 | use crate::memory::{PhysicalAddress, VirtualAddress}; 6 | 7 | pub mod block; 8 | pub mod bus; 9 | pub mod device_tree; 10 | pub mod driver; 11 | 12 | /// 从设备树的物理地址来获取全部设备信息并初始化 13 | pub fn init(dtb_pa: PhysicalAddress) { 14 | let dtb_va = VirtualAddress::from(dtb_pa); 15 | device_tree::init(dtb_va); 16 | println!("mod driver initialized") 17 | } 18 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter07-experssion/experssion.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let x = 5u32; 3 | 4 | x; 5 | x + 1; 6 | 15; 7 | 8 | let y = { 9 | let x_squared = x * x; 10 | let x_cube = x_squared * x; 11 | 12 | x_cube + x_squared + x 13 | }; 14 | 15 | let z = { 16 | 2 * x; 17 | }; 18 | 19 | println!("x is {:?}", x); 20 | println!("y is {:?}", y); 21 | println!("z is {:?}", z); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab1/os/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "os" 3 | version = "0.1.0" 4 | authors = ["Yukiteru Lee "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] } 11 | 12 | [profile.dev] 13 | panic = "abort" 14 | 15 | [profile.release] 16 | panic = "abort" 17 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter15-scope/scope_02.rs: -------------------------------------------------------------------------------- 1 | fn destroy_box(c: Box) { 2 | println!("Destroying a box that contains {}", c); 3 | } 4 | 5 | fn main() { 6 | let x = 5u32; 7 | let y = x; 8 | println!("x is {}, and y is {}", x, y); 9 | 10 | let a = Box::new(5i32); 11 | println!("a contains: {}", a); 12 | let b = a; 13 | // println!("a contains: {}", a); 14 | destroy_box(b); 15 | // println!("b contains: {}", b); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /02-modular-programing/02-csv-read/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "csv-read" 5 | version = "0.1.0" 6 | dependencies = [ 7 | "linked-list", 8 | ] 9 | 10 | [[package]] 11 | name = "linked-list" 12 | version = "0.0.3" 13 | source = "registry+https://github.com/rust-lang/crates.io-index" 14 | checksum = "a4dacf969043dc69f1f731b5042eb05e030d264bcf34f2242889fcbdc7a65f06" 15 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/enums/enums1.rs: -------------------------------------------------------------------------------- 1 | // enums1.rs 2 | // Make me compile! Execute `rustlings hint enums1` for hints! 3 | 4 | #[derive(Debug)] 5 | enum Message { 6 | // TODO: define a few types of messages as used below 7 | Quit, Echo, Move, ChangeColor 8 | } 9 | 10 | fn main() { 11 | println!("{:?}", Message::Quit); 12 | println!("{:?}", Message::Echo); 13 | println!("{:?}", Message::Move); 14 | println!("{:?}", Message::ChangeColor); 15 | } 16 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter04-variable_bindings/variable_bindings_1.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let an_integer = 1u32; 3 | let a_boolean = true; 4 | let unit = (); 5 | 6 | let copied_integer = an_integer; 7 | 8 | println!("An integer: {:?}", copied_integer); 9 | println!("A boolean: {:?}", a_boolean); 10 | println!("Meet the unit value: {:?}", unit); 11 | 12 | let _unused_variable = 3u32; 13 | 14 | let _noisy_unused_variable = 2u32; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter08-flow_control/flow_control_11.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | struct Foo { x: (u32, u32), y: u32 } 3 | 4 | let foo = Foo { x: (1, 2), y: 3 }; 5 | let Foo { x: (a, b), y } = foo; 6 | 7 | println!("a = {}, b = {}, y = {} ", a, b, y); 8 | 9 | let Foo { y: i, x: j } = foo; 10 | println!("i = {:?}, j = {:?}", i, j); 11 | 12 | let Foo { y, .. } = foo; 13 | println!("y = {}", y); 14 | 15 | // let Foo { y } = foo; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter14-generics/generics_03.rs: -------------------------------------------------------------------------------- 1 | struct Val { 2 | val: f64 3 | } 4 | 5 | struct GenVal{ 6 | gen_val: T 7 | } 8 | 9 | impl Val { 10 | fn value(&self) -> &f64 { &self.val } 11 | } 12 | 13 | impl GenVal { 14 | fn value(&self) -> &T { &self.gen_val } 15 | } 16 | 17 | fn main() { 18 | let x = Val { val: 3.0 }; 19 | let y = GenVal { gen_val: 3i32 }; 20 | 21 | println!("{}, {}", x.value(), y.value()); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/macros/README.md: -------------------------------------------------------------------------------- 1 | ### Macros 2 | 3 | Rust's macro system is very powerful, but also kind of difficult to wrap your 4 | head around. We're not going to teach you how to write your own fully-featured 5 | modules, instead we'll show you how to use and create them. 6 | 7 | #### Book Sections 8 | 9 | - [Macros](https://doc.rust-lang.org/book/ch19-06-macros.html) 10 | - [The Little Book of Rust Macros](https://danielkeep.github.io/tlborm/book/index.html) 11 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab2/os/src/algorithm/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "algorithm" 5 | version = "0.1.0" 6 | dependencies = [ 7 | "bit_field", 8 | ] 9 | 10 | [[package]] 11 | name = "bit_field" 12 | version = "0.10.0" 13 | source = "registry+https://github.com/rust-lang/crates.io-index" 14 | checksum = "a165d606cf084741d4ac3a28fb6e9b1eb0bd31f6cd999098cfddb0b2ab381dc0" 15 | -------------------------------------------------------------------------------- /02-modular-programing/06-static_hashmap/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "lazy_static" 5 | version = "1.4.0" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 8 | 9 | [[package]] 10 | name = "static_hashmap" 11 | version = "0.1.0" 12 | dependencies = [ 13 | "lazy_static", 14 | ] 15 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/clippy/README.md: -------------------------------------------------------------------------------- 1 | ### Clippy 2 | 3 | The Clippy tool is a collection of lints to analyze your code so you can catch common mistakes and improve your Rust code. 4 | 5 | If you used the installation script for Rustlings, Clippy should be already installed. 6 | If not you can install it manually via `rustup component add clippy`. 7 | 8 | For more information about Clippy lints, please see [their documentation page](https://rust-lang.github.io/rust-clippy/master/). 9 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4/os/src/process/mod.rs: -------------------------------------------------------------------------------- 1 | //! 管理进程 / 线程 2 | 3 | mod config; 4 | mod kernel_stack; 5 | #[allow(clippy::module_inception)] 6 | mod process; 7 | mod processor; 8 | mod thread; 9 | 10 | use crate::interrupt::*; 11 | use crate::memory::*; 12 | use alloc::sync::Arc; 13 | use spin::{Mutex, RwLock}; 14 | 15 | pub use config::*; 16 | pub use kernel_stack::KERNEL_STACK; 17 | pub use process::Process; 18 | pub use processor::PROCESSOR; 19 | pub use thread::Thread; 20 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab5/os/src/process/mod.rs: -------------------------------------------------------------------------------- 1 | //! 管理进程 / 线程 2 | 3 | mod config; 4 | mod kernel_stack; 5 | #[allow(clippy::module_inception)] 6 | mod process; 7 | mod processor; 8 | mod thread; 9 | 10 | use crate::interrupt::*; 11 | use crate::memory::*; 12 | use alloc::sync::Arc; 13 | use spin::{Mutex, RwLock}; 14 | 15 | pub use config::*; 16 | pub use kernel_stack::KERNEL_STACK; 17 | pub use process::Process; 18 | pub use processor::PROCESSOR; 19 | pub use thread::Thread; 20 | -------------------------------------------------------------------------------- /02-modular-programing/07-static_hashmap-2015/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "lazy_static" 5 | version = "1.4.0" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 8 | 9 | [[package]] 10 | name = "static_hashmap" 11 | version = "0.1.0" 12 | dependencies = [ 13 | "lazy_static", 14 | ] 15 | -------------------------------------------------------------------------------- /02-modular-programing/08-static_hashmap-2018/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "lazy_static" 5 | version = "1.4.0" 6 | source = "registry+https://github.com/rust-lang/crates.io-index" 7 | checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" 8 | 9 | [[package]] 10 | name = "static_hashmap" 11 | version = "0.1.0" 12 | dependencies = [ 13 | "lazy_static", 14 | ] 15 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_1/os/src/algorithm/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "algorithm" 5 | version = "0.1.0" 6 | dependencies = [ 7 | "bit_field", 8 | ] 9 | 10 | [[package]] 11 | name = "bit_field" 12 | version = "0.10.0" 13 | source = "registry+https://github.com/rust-lang/crates.io-index" 14 | checksum = "a165d606cf084741d4ac3a28fb6e9b1eb0bd31f6cd999098cfddb0b2ab381dc0" 15 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter03-custom_types/custom_types_4.rs: -------------------------------------------------------------------------------- 1 | enum Number { 2 | Zero, 3 | One, 4 | Two, 5 | } 6 | 7 | enum Color { 8 | Red = 0xff0000, 9 | Green = 0x00ff00, 10 | Blue = 0x0000ff, 11 | } 12 | 13 | fn main() { 14 | println!("zero is {}", Number::Zero as i32); 15 | println!("one is {}", Number::One as i32); 16 | 17 | println!("roses are #{:06x}", Color::Red as i32); 18 | println!("violets are #{:06x}", Color::Blue as i32); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter08-flow_control/flow_control_02.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let mut count = 0u32; 3 | println!("Let's count until infinity!"); 4 | 5 | loop { 6 | count += 1; 7 | 8 | if count == 3 { 9 | println!("three"); 10 | continue; 11 | } 12 | 13 | println!("{}", count); 14 | 15 | if count == 5 { 16 | println!("OK, that's enough"); 17 | break; 18 | } 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /02-modular-programing/10-csv_challenge/src/lib.rs: -------------------------------------------------------------------------------- 1 | //! This is documentation for the `csv_challenge` lib crate 2 | //! 3 | //! Usage: 4 | //! ``` 5 | //! use csv_challenge::{ 6 | //! Opt, 7 | //! {load_csv, write_csv}, 8 | //! replace_column, 9 | //! }; 10 | //! ``` 11 | 12 | mod opt; 13 | mod err; 14 | mod core; 15 | 16 | pub use self::opt::Opt; 17 | pub use self::core::{ 18 | read::{load_csv, write_csv}, 19 | write::replace_column 20 | }; 21 | 22 | -------------------------------------------------------------------------------- /04-learn-rust-the-hard-way/ex33.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let mut i = 0; 3 | let mut numbers: Vec = Vec::new(); 4 | while i < 6 { 5 | println!("At the top i is {}", i); 6 | numbers.push(i); 7 | 8 | i = i + 1; 9 | println!("Numbers now: {:?}", numbers); 10 | println!("At the bottom i is {}", i); 11 | } 12 | 13 | println!("The numbers: "); 14 | 15 | for num in numbers { 16 | println!("{}", num); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_1/os/src/process/mod.rs: -------------------------------------------------------------------------------- 1 | //! 管理进程 / 线程 2 | 3 | mod config; 4 | mod kernel_stack; 5 | #[allow(clippy::module_inception)] 6 | mod process; 7 | mod processor; 8 | mod thread; 9 | 10 | use crate::interrupt::*; 11 | use crate::memory::*; 12 | use alloc::sync::Arc; 13 | use spin::{Mutex, RwLock}; 14 | 15 | pub use config::*; 16 | pub use kernel_stack::KERNEL_STACK; 17 | pub use process::Process; 18 | pub use processor::PROCESSOR; 19 | pub use thread::Thread; 20 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_2/os/src/process/mod.rs: -------------------------------------------------------------------------------- 1 | //! 管理进程 / 线程 2 | 3 | mod config; 4 | mod kernel_stack; 5 | #[allow(clippy::module_inception)] 6 | mod process; 7 | mod processor; 8 | mod thread; 9 | 10 | use crate::interrupt::*; 11 | use crate::memory::*; 12 | use alloc::sync::Arc; 13 | use spin::{Mutex, RwLock}; 14 | 15 | pub use config::*; 16 | pub use kernel_stack::KERNEL_STACK; 17 | pub use process::Process; 18 | pub use processor::PROCESSOR; 19 | pub use thread::Thread; 20 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter09-fn/fn_09.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let vec1 = vec![1, 2, 3]; 3 | let vec2 = vec![4, 5, 6]; 4 | 5 | println!("2 in vec1: {}", vec1.iter() .any(|&x| x == 2)); 6 | println!("2 in vec2: {}", vec2.into_iter().any(| x| x == 2)); 7 | 8 | let array1 = [1, 2, 3]; 9 | let array2 = [4, 5, 6]; 10 | 11 | println!("2 in array1: {}", array1.iter() .any(|&x| x == 2)); 12 | println!("2 in array2: {}", array2.into_iter().any(|&x| x == 2)); 13 | } 14 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter15-scope/scope_15.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::Debug; 2 | 3 | #[derive(Debug)] 4 | struct Ref<'a, T: 'a>(&'a T); 5 | 6 | fn print(t: T) where 7 | T: Debug { 8 | println!("`print`: t is {:?}", t); 9 | } 10 | 11 | fn print_ref<'a, T>(t: &'a T) where 12 | T: Debug + 'a { 13 | println!("`print_ref`: t is {:?}", t); 14 | } 15 | 16 | fn main() { 17 | let x = 7; 18 | let ref_x = Ref(&x); 19 | 20 | print_ref(&ref_x); 21 | print(ref_x); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/os/src/process/mod.rs: -------------------------------------------------------------------------------- 1 | //! 管理进程 / 线程 2 | 3 | mod config; 4 | mod kernel_stack; 5 | #[allow(clippy::module_inception)] 6 | mod process; 7 | mod processor; 8 | mod thread; 9 | 10 | use crate::interrupt::*; 11 | use crate::memory::*; 12 | use alloc::{sync::Arc, vec, vec::Vec}; 13 | use spin::{Mutex, RwLock}; 14 | 15 | pub use config::*; 16 | pub use kernel_stack::KERNEL_STACK; 17 | pub use process::Process; 18 | pub use processor::PROCESSOR; 19 | pub use thread::Thread; 20 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter18-error/error_05.rs: -------------------------------------------------------------------------------- 1 | fn multiply(first_number_str: &str, second_number_str: &str) -> i32 { 2 | let first_number = first_number_str.parse::().unwrap(); 3 | let second_number = second_number_str.parse::().unwrap(); 4 | first_number * second_number 5 | } 6 | 7 | fn main() { 8 | let twenty = multiply("10", "2"); 9 | println!("double is {}", twenty); 10 | 11 | let tt = multiply("t", "2"); 12 | println!("double is {}", tt); 13 | } 14 | 15 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter09-fn/fn_12.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | fn sum_odd_numbers(up_to: u32) -> u32 { 3 | let mut acc = 0; 4 | for i in 0..up_to { 5 | let addition: u32 = match i%2 == 1 { 6 | true => i, 7 | false => continue, 8 | // ??? 9 | }; 10 | acc += addition; 11 | } 12 | acc 13 | } 14 | println!("Sum of odd numbers up to 9 (excluding): {}", sum_odd_numbers(9)); 15 | } 16 | 17 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter15-scope/scope_16.rs: -------------------------------------------------------------------------------- 1 | fn multiply<'a>(first: &'a i32, second: &'a i32) -> i32 { 2 | first * second 3 | } 4 | 5 | // what??? 6 | fn choose_first<'a: 'b, 'b>(first: &'a i32, _: &'b i32) -> &'b i32 { 7 | first 8 | } 9 | 10 | fn main() { 11 | let first = 2; 12 | 13 | { 14 | let second = 3; 15 | 16 | println!("The product is {}", multiply(&first, &second)); 17 | println!("{} is the first", choose_first(&first, &second)); 18 | }; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /02-modular-programing/08-static_hashmap-2018/src/main.rs: -------------------------------------------------------------------------------- 1 | // src/main.rs 2 | mod read_func; 3 | use crate::read_func::{read_kv, rw_mut_kv}; 4 | 5 | fn main() { 6 | read_kv(); 7 | match rw_mut_kv() { 8 | Ok(()) => { 9 | let m = read_func::static_kv::MAP_MUT 10 | .read().map_err(|e| e.to_string()).unwrap(); 11 | assert_eq!("baz", *m.get(&1).unwrap_or(&read_func::static_kv::NF)); 12 | }, 13 | Err(e) => { println!("Error {}", e) } 14 | } 15 | } 16 | 17 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/move_semantics/README.md: -------------------------------------------------------------------------------- 1 | ### Move Semantics 2 | 3 | These exercises are adapted from [pnkfelix](https://github.com/pnkfelix)'s [Rust Tutorial](https://pnkfelix.github.io/rust-examples-icfp2014/) -- Thank you Felix!!! 4 | 5 | #### Book Sections 6 | 7 | For this section, the book links are especially important. 8 | 9 | - [Ownership](https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html) 10 | - [Reference and borrowing](https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html) 11 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/tests/tests1.rs: -------------------------------------------------------------------------------- 1 | // tests1.rs 2 | // Tests are important to ensure that your code does what you think it should do. 3 | // Tests can be run on this file with the following command: 4 | // rustlings run tests1 5 | 6 | // This test has a problem with it -- make the test compile! Make the test 7 | // pass! Make the test fail! Execute `rustlings hint tests1` for hints :) 8 | 9 | #[cfg(test)] 10 | mod tests { 11 | #[test] 12 | fn you_can_assert() { 13 | assert!(true); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter15-scope/scope_01.rs: -------------------------------------------------------------------------------- 1 | fn create_box() { 2 | let _box1 = Box::new(3i32); 3 | } 4 | 5 | struct ToDrop; 6 | 7 | impl Drop for ToDrop { 8 | fn drop(&mut self) { 9 | println!("ToDrop is being dropped"); 10 | } 11 | } 12 | 13 | fn main() { 14 | let _box2 = Box::new(5i32); 15 | { 16 | let _box3 = Box::new(4i32); 17 | } 18 | for _ in 0u32..1_000 { 19 | create_box(); 20 | } 21 | 22 | let x = ToDrop; 23 | println!("Made a ToDrop"); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter18-error/error_16.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let strings = vec!["tofu", "93", "18"]; 3 | let (numbers, errors): (Vec<_>, Vec<_>) = strings 4 | .into_iter() 5 | .map(|s| s.parse::()) 6 | .partition(Result::is_ok); 7 | let numbers: Vec<_> = numbers.into_iter().map(Result::unwrap).collect(); 8 | let errors: Vec<_> = errors.into_iter().map(Result::unwrap_err).collect(); 9 | println!("Numbers: {:?}", numbers); 10 | println!("Errors: {:?}", errors); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /02-modular-programing/09-csv_challenge/src/opt.rs: -------------------------------------------------------------------------------- 1 | use structopt_derive::*; 2 | #[derive(StructOpt, Debug)] 3 | #[structopt(name = "csv_challenge", about = "Usage")] 4 | pub struct Opt { 5 | #[structopt(help = "Input file")] 6 | pub input: String, 7 | #[structopt(help = "Column Name")] 8 | pub column_name: String, 9 | #[structopt(help = "Replacement Column Name")] 10 | pub replacement: String, 11 | #[structopt(help = "Output file, stdout if not present")] 12 | pub output: Option, 13 | } 14 | -------------------------------------------------------------------------------- /02-modular-programing/10-csv_challenge/src/opt.rs: -------------------------------------------------------------------------------- 1 | use structopt_derive::*; 2 | #[derive(StructOpt, Debug)] 3 | #[structopt(name = "csv_challenge", about = "Usage")] 4 | pub struct Opt { 5 | #[structopt(help = "Input file")] 6 | pub input: String, 7 | #[structopt(help = "Column Name")] 8 | pub column_name: String, 9 | #[structopt(help = "Replacement Column Name")] 10 | pub replacement: String, 11 | #[structopt(help = "Output file, stdout if not present")] 12 | pub output: Option, 13 | } 14 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter09-fn/fn_03.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | fn function (i: i32) -> i32 { i + 1 } 3 | 4 | let closure_annotated = |i: i32| -> i32 { i + 1 }; 5 | let closure_inferred = |i | i + 1 ; 6 | 7 | let i = 1; 8 | println!("function: {}", function(i)); 9 | println!("closure_annotated: {}", closure_annotated(i)); 10 | println!("closure_inferred: {}", closure_inferred(i)); 11 | 12 | let one = || 1; 13 | println!("closure returning one: {}", one()); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/variables/variables1.rs: -------------------------------------------------------------------------------- 1 | // variables1.rs 2 | // Make me compile! Execute the command `rustlings hint variables1` if you want a hint :) 3 | 4 | // About this `I AM NOT DONE` thing: 5 | // We sometimes encourage you to keep trying things on a given exercise, 6 | // even after you already figured it out. If you got everything working and 7 | // feel ready for the next exercise, remove the `I AM NOT DONE` comment below. 8 | 9 | fn main() { 10 | let x = 5; 11 | println!("x has the value {}", x); 12 | } 13 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/os/src/process/mod.rs: -------------------------------------------------------------------------------- 1 | //! 管理进程 / 线程 2 | 3 | mod config; 4 | mod kernel_stack; 5 | mod lock; 6 | #[allow(clippy::module_inception)] 7 | mod process; 8 | mod processor; 9 | mod thread; 10 | 11 | use crate::interrupt::*; 12 | use crate::memory::*; 13 | use alloc::{sync::Arc, vec, vec::Vec}; 14 | use spin::Mutex; 15 | 16 | pub use config::*; 17 | pub use kernel_stack::KERNEL_STACK; 18 | pub use lock::Lock; 19 | pub use process::Process; 20 | pub use processor::PROCESSOR; 21 | pub use thread::Thread; 22 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/os/src/process/mod.rs: -------------------------------------------------------------------------------- 1 | //! 管理进程 / 线程 2 | 3 | mod config; 4 | mod kernel_stack; 5 | mod lock; 6 | #[allow(clippy::module_inception)] 7 | mod process; 8 | mod processor; 9 | mod thread; 10 | 11 | use crate::interrupt::*; 12 | use crate::memory::*; 13 | use alloc::{sync::Arc, vec, vec::Vec}; 14 | use spin::Mutex; 15 | 16 | pub use config::*; 17 | pub use kernel_stack::KERNEL_STACK; 18 | pub use lock::Lock; 19 | pub use process::Process; 20 | pub use processor::PROCESSOR; 21 | pub use thread::Thread; 22 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/os/src/process/mod.rs: -------------------------------------------------------------------------------- 1 | //! 管理进程 / 线程 2 | 3 | mod config; 4 | mod kernel_stack; 5 | mod lock; 6 | #[allow(clippy::module_inception)] 7 | mod process; 8 | mod processor; 9 | mod thread; 10 | 11 | use crate::interrupt::*; 12 | use crate::memory::*; 13 | use alloc::{sync::Arc, vec, vec::Vec}; 14 | use spin::Mutex; 15 | 16 | pub use config::*; 17 | pub use kernel_stack::KERNEL_STACK; 18 | pub use lock::Lock; 19 | pub use process::Process; 20 | pub use processor::PROCESSOR; 21 | pub use thread::Thread; 22 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3/os/src/memory/mapping/mod.rs: -------------------------------------------------------------------------------- 1 | //! 内存映射 2 | //! 3 | //! 每个线程保存一个 [`Mapping`],其中记录了所有的字段 [`Segment`]。 4 | //! 同时,也要追踪为页表或字段分配的所有物理页,目的是 drop 掉之后可以安全释放所有资源。 5 | 6 | #[allow(clippy::module_inception)] 7 | mod mapping; 8 | mod memory_set; 9 | mod page_table; 10 | mod page_table_entry; 11 | mod segment; 12 | 13 | pub use mapping::Mapping; 14 | pub use memory_set::MemorySet; 15 | pub use page_table::{PageTable, PageTableTracker}; 16 | pub use page_table_entry::{Flags, PageTableEntry}; 17 | pub use segment::{MapType, Segment}; 18 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4/os/src/memory/mapping/mod.rs: -------------------------------------------------------------------------------- 1 | //! 内存映射 2 | //! 3 | //! 每个线程保存一个 [`Mapping`],其中记录了所有的字段 [`Segment`]。 4 | //! 同时,也要追踪为页表或字段分配的所有物理页,目的是 drop 掉之后可以安全释放所有资源。 5 | 6 | #[allow(clippy::module_inception)] 7 | mod mapping; 8 | mod memory_set; 9 | mod page_table; 10 | mod page_table_entry; 11 | mod segment; 12 | 13 | pub use mapping::Mapping; 14 | pub use memory_set::MemorySet; 15 | pub use page_table::{PageTable, PageTableTracker}; 16 | pub use page_table_entry::{Flags, PageTableEntry}; 17 | pub use segment::{MapType, Segment}; 18 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab5/os/src/memory/mapping/mod.rs: -------------------------------------------------------------------------------- 1 | //! 内存映射 2 | //! 3 | //! 每个线程保存一个 [`Mapping`],其中记录了所有的字段 [`Segment`]。 4 | //! 同时,也要追踪为页表或字段分配的所有物理页,目的是 drop 掉之后可以安全释放所有资源。 5 | 6 | #[allow(clippy::module_inception)] 7 | mod mapping; 8 | mod memory_set; 9 | mod page_table; 10 | mod page_table_entry; 11 | mod segment; 12 | 13 | pub use mapping::Mapping; 14 | pub use memory_set::MemorySet; 15 | pub use page_table::{PageTable, PageTableTracker}; 16 | pub use page_table_entry::{Flags, PageTableEntry}; 17 | pub use segment::{MapType, Segment}; 18 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/os/src/memory/mapping/mod.rs: -------------------------------------------------------------------------------- 1 | //! 内存映射 2 | //! 3 | //! 每个线程保存一个 [`Mapping`],其中记录了所有的字段 [`Segment`]。 4 | //! 同时,也要追踪为页表或字段分配的所有物理页,目的是 drop 掉之后可以安全释放所有资源。 5 | 6 | #[allow(clippy::module_inception)] 7 | mod mapping; 8 | mod memory_set; 9 | mod page_table; 10 | mod page_table_entry; 11 | mod segment; 12 | 13 | pub use mapping::Mapping; 14 | pub use memory_set::MemorySet; 15 | pub use page_table::{PageTable, PageTableTracker}; 16 | pub use page_table_entry::{Flags, PageTableEntry}; 17 | pub use segment::{MapType, Segment}; 18 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter17-macro/macro_7.rs: -------------------------------------------------------------------------------- 1 | macro_rules! calculate { 2 | (eval $e:expr) => {{ 3 | { 4 | let val: usize = $e; 5 | println!("{} = {}", stringify!{$e}, val); 6 | } 7 | }}; 8 | 9 | (eval $e:expr, $(eval $es:expr),+) => {{ 10 | calculate! { eval $e } 11 | calculate! { $(eval $es),+ } 12 | }}; 13 | } 14 | 15 | fn main() { 16 | // 原文里的注释有点好笑 17 | calculate! { 18 | eval 1 + 2, 19 | eval 3 + 4, 20 | eval (2 * 3) + 1 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /02-modular-programing/03-use_regex/src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate regex; 2 | use regex::Regex; 3 | 4 | const TO_SEARCH: &'static str = " 5 | On 2017-12-31, happy. On 2018-01-01, New Year. 6 | "; 7 | 8 | fn main() { 9 | let re = Regex::new(r"(\d{4})-(\d{2})-(\d{2})").unwrap(); 10 | for caps in re.captures_iter(TO_SEARCH) { 11 | println!("year: {}, month: {}, day: {}", 12 | caps.get(1).unwrap().as_str(), 13 | caps.get(2).unwrap().as_str(), 14 | caps.get(3).unwrap().as_str()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/os/src/memory/mapping/mod.rs: -------------------------------------------------------------------------------- 1 | //! 内存映射 2 | //! 3 | //! 每个线程保存一个 [`Mapping`],其中记录了所有的字段 [`Segment`]。 4 | //! 同时,也要追踪为页表或字段分配的所有物理页,目的是 drop 掉之后可以安全释放所有资源。 5 | 6 | #[allow(clippy::module_inception)] 7 | mod mapping; 8 | mod memory_set; 9 | mod page_table; 10 | mod page_table_entry; 11 | mod segment; 12 | 13 | pub use mapping::Mapping; 14 | pub use memory_set::MemorySet; 15 | pub use page_table::{PageTable, PageTableTracker}; 16 | pub use page_table_entry::{Flags, PageTableEntry}; 17 | pub use segment::{MapType, Segment}; 18 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/os/src/memory/mapping/mod.rs: -------------------------------------------------------------------------------- 1 | //! 内存映射 2 | //! 3 | //! 每个线程保存一个 [`Mapping`],其中记录了所有的字段 [`Segment`]。 4 | //! 同时,也要追踪为页表或字段分配的所有物理页,目的是 drop 掉之后可以安全释放所有资源。 5 | 6 | #[allow(clippy::module_inception)] 7 | mod mapping; 8 | mod memory_set; 9 | mod page_table; 10 | mod page_table_entry; 11 | mod segment; 12 | 13 | pub use mapping::Mapping; 14 | pub use memory_set::MemorySet; 15 | pub use page_table::{PageTable, PageTableTracker}; 16 | pub use page_table_entry::{Flags, PageTableEntry}; 17 | pub use segment::{MapType, Segment}; 18 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter08-flow_control/flow_control_07.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let number = 13; 3 | 4 | println!("Tell me about {}", number); 5 | match number { 6 | 1 => println!("One!"), 7 | 2 | 3 | 5 | 7 | 11 => println!("This is a prime"), 8 | 13..=19 => println!("A teen"), 9 | _ => println!("Ain't special"), 10 | } 11 | 12 | let boolean = true; 13 | let binary = match boolean { 14 | false => 0, 15 | true => 1, 16 | }; 17 | 18 | println!("{} -> {}", boolean, binary); 19 | } 20 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/primitive_types/primitive_types1.rs: -------------------------------------------------------------------------------- 1 | // primitive_types1.rs 2 | // Fill in the rest of the line that has code missing! 3 | // No hints, there's no tricks, just get used to typing these :) 4 | 5 | fn main() { 6 | // Booleans (`bool`) 7 | 8 | let is_morning = true; 9 | if is_morning { 10 | println!("Good morning!"); 11 | } 12 | 13 | let is_evening = true; // Finish the rest of this line like the example! Or make it be false! 14 | if is_evening { 15 | println!("Good evening!"); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/standard_library_types/README.md: -------------------------------------------------------------------------------- 1 | For the Arc exercise check out the chapter [Shared-State Concurrency](https://doc.rust-lang.org/book/ch16-03-shared-state.html) of the Rust Book. 2 | 3 | For the Iterator exercise check out the chapters [Iterator](https://doc.rust-lang.org/book/ch13-02-iterators.html) of the Rust Book and the [Iterator documentation](https://doc.rust-lang.org/stable/std/iter/). 4 | Do not adjust your monitors-- iterators1.rs is indeed missing. Iterators is a challenging topic, so we're leaving space for a simpler exercise! 5 | 6 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_1/os/src/memory/mapping/mod.rs: -------------------------------------------------------------------------------- 1 | //! 内存映射 2 | //! 3 | //! 每个线程保存一个 [`Mapping`],其中记录了所有的字段 [`Segment`]。 4 | //! 同时,也要追踪为页表或字段分配的所有物理页,目的是 drop 掉之后可以安全释放所有资源。 5 | 6 | #[allow(clippy::module_inception)] 7 | mod mapping; 8 | mod memory_set; 9 | mod page_table; 10 | mod page_table_entry; 11 | mod segment; 12 | 13 | pub use mapping::Mapping; 14 | pub use memory_set::MemorySet; 15 | pub use page_table::{PageTable, PageTableTracker}; 16 | pub use page_table_entry::{Flags, PageTableEntry}; 17 | pub use segment::{MapType, Segment}; 18 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_2/os/src/memory/mapping/mod.rs: -------------------------------------------------------------------------------- 1 | //! 内存映射 2 | //! 3 | //! 每个线程保存一个 [`Mapping`],其中记录了所有的字段 [`Segment`]。 4 | //! 同时,也要追踪为页表或字段分配的所有物理页,目的是 drop 掉之后可以安全释放所有资源。 5 | 6 | #[allow(clippy::module_inception)] 7 | mod mapping; 8 | mod memory_set; 9 | mod page_table; 10 | mod page_table_entry; 11 | mod segment; 12 | 13 | pub use mapping::Mapping; 14 | pub use memory_set::MemorySet; 15 | pub use page_table::{PageTable, PageTableTracker}; 16 | pub use page_table_entry::{Flags, PageTableEntry}; 17 | pub use segment::{MapType, Segment}; 18 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter18-error/error_10.rs: -------------------------------------------------------------------------------- 1 | fn double_first(vec: Vec<&str>) -> i32 { 2 | let first = vec.first().unwrap(); 3 | 2 * first.parse::().unwrap() 4 | } 5 | 6 | fn main() { 7 | let numbers = vec!["42", "93", "18"]; 8 | let empty = vec![]; 9 | let strings = vec!["tofu", "93", "18"]; 10 | 11 | println!("The first doubled is {}", double_first(numbers)); 12 | 13 | println!("The first doubled is {}", double_first(empty)); 14 | 15 | println!("The first doubled is {}", double_first(strings)); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/tests/tests3.rs: -------------------------------------------------------------------------------- 1 | // tests3.rs 2 | // This test isn't testing our function -- make it do that in such a way that 3 | // the test passes. Then write a second test that tests whether we get the result 4 | // we expect to get when we call `is_even(5)`. 5 | // Execute `rustlings hint tests3` for hints :) 6 | 7 | pub fn is_even(num: i32) -> bool { 8 | num % 2 == 0 9 | } 10 | 11 | #[cfg(test)] 12 | mod tests { 13 | use super::*; 14 | 15 | #[test] 16 | fn is_true_when_even() { 17 | assert!(is_even(4)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /04-learn-rust-the-hard-way/ex09.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let days = "Mon Tue Wed Thu Fri Sat Sun"; 3 | let months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"; 4 | 5 | println!("Here are the days: {}", days); 6 | println!("Here are the months: {}", months); 7 | 8 | // Rust doesn't support this grammar 9 | // 10 | // println!(""" 11 | // There's something going on here. 12 | // With the three double-quotes. 13 | // We'll be able to type as much as we like. 14 | // Even 4 lines if we want, or 5, or 6. 15 | // """); 16 | } 17 | -------------------------------------------------------------------------------- /04-learn-rust-the-hard-way/ex18.rs: -------------------------------------------------------------------------------- 1 | fn print_two(arg1: &str, arg2: &str) { 2 | println!("arg1: {:?}, arg2: {:?}", arg1, arg2); 3 | } 4 | 5 | fn print_two_again(arg1: &str, arg2: &str) { 6 | println!("arg1: {:?}, arg2: {:?}", arg1, arg2); 7 | } 8 | 9 | fn print_one(arg1: &str) { 10 | println!("arg1: {:?}", arg1); 11 | } 12 | 13 | fn print_none() { 14 | println!("I got nothing."); 15 | } 16 | 17 | fn main() { 18 | print_two("Zed", "Shaw"); 19 | print_two_again("Zed", "Shaw"); 20 | print_one("First!"); 21 | print_none(); 22 | } 23 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/os/src/kernel/thread.rs: -------------------------------------------------------------------------------- 1 | //! 线程相关的内核功能 2 | 3 | use super::*; 4 | 5 | pub(super) fn sys_gettid() -> SyscallResult { 6 | SyscallResult::Proceed(PROCESSOR.lock().current_thread().id) 7 | } 8 | 9 | pub(super) fn sys_clone(context: &Context) -> SyscallResult { 10 | let thread = PROCESSOR.lock().current_thread().clone(); 11 | let new_thread = thread.fork(&context).unwrap(); 12 | new_thread.inner().context.unwrap().x[10] = 0; 13 | PROCESSOR.lock().add_thread(new_thread); 14 | SyscallResult::Proceed(thread.id) 15 | } 16 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/os/src/kernel/thread.rs: -------------------------------------------------------------------------------- 1 | //! 线程相关的内核功能 2 | 3 | use super::*; 4 | 5 | pub(super) fn sys_gettid() -> SyscallResult { 6 | SyscallResult::Proceed(PROCESSOR.lock().current_thread().id) 7 | } 8 | 9 | pub(super) fn sys_clone(context: &Context) -> SyscallResult { 10 | let thread = PROCESSOR.lock().current_thread().clone(); 11 | let new_thread = thread.fork(&context).unwrap(); 12 | new_thread.inner().context.unwrap().x[10] = 0; 13 | PROCESSOR.lock().add_thread(new_thread); 14 | SyscallResult::Proceed(thread.id) 15 | } 16 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter05-types/types_2.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let x = 1u8; 3 | let y = 2u32; 4 | let z = 3f32; 5 | 6 | let i = 1; // i32 7 | let f = 1.0; // f64 8 | 9 | println!("size of `x` in bytes: {}", std::mem::size_of_val(&x)); 10 | println!("size of `y` in bytes: {}", std::mem::size_of_val(&y)); 11 | println!("size of `z` in bytes: {}", std::mem::size_of_val(&z)); 12 | println!("size of `i` in bytes: {}", std::mem::size_of_val(&i)); 13 | println!("size of `f` in bytes: {}", std::mem::size_of_val(&f)); 14 | } 15 | 16 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter13-attribute/attribute_3.rs: -------------------------------------------------------------------------------- 1 | #[cfg(target_os = "linux")] 2 | fn are_you_on_linux() { 3 | println!("You are running linux!") 4 | } 5 | 6 | #[cfg(not(target_os = "linux"))] 7 | fn are_you_on_linux() { 8 | println!("You are *not* running linux!") 9 | } 10 | 11 | fn main() { 12 | are_you_on_linux(); 13 | 14 | println!("Are you sure?"); 15 | if cfg!(target_os = "linux") { 16 | println!("Yes. It's definitely linux!"); 17 | } else { 18 | println!("Yes. It's definitely *not* linux!"); 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter15-scope/scope_18.rs: -------------------------------------------------------------------------------- 1 | fn elided_input(x: &i32) { 2 | println!("`elided_input`: {}", x) 3 | } 4 | 5 | fn annotated_input<'a>(x: &'a i32) { 6 | println!("`annotated_input`: {}", x) 7 | } 8 | 9 | fn elided_pass(x: &i32) -> &i32 { x } 10 | 11 | fn annotated_pass<'a>(x: &'a i32) -> &'a i32 { x } 12 | 13 | fn main() { 14 | let x = 3; 15 | 16 | elided_input(&x); 17 | annotated_input(&x); 18 | 19 | println!("`elided_pass`: {}", elided_pass(&x)); 20 | println!("`annotated_pass`: {}", annotated_pass(&x)); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab0/os/src/linker.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_ARCH(riscv) 2 | 3 | ENTRY(_start) 4 | 5 | BASE_ADDRESS = 0x80200000; 6 | 7 | SECTIONS 8 | { 9 | . = BASE_ADDRESS; 10 | kernel_start = .; 11 | text_start = .; 12 | 13 | .text : { 14 | *(.text.entry) 15 | *(.text .text.*) 16 | } 17 | 18 | rodata_start = .; 19 | 20 | .rodata : { 21 | *(.rodata .rodata.*) 22 | } 23 | 24 | data_start = .; 25 | 26 | .data : { 27 | *(.data .data.*) 28 | } 29 | 30 | bss_start = .; 31 | 32 | .bss : { 33 | *(.snss .bss .bss.*) 34 | } 35 | 36 | kernel_end = .; 37 | } 38 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab1/os/src/linker.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_ARCH(riscv) 2 | 3 | ENTRY(_start) 4 | 5 | BASE_ADDRESS = 0x80200000; 6 | 7 | SECTIONS 8 | { 9 | . = BASE_ADDRESS; 10 | kernel_start = .; 11 | text_start = .; 12 | 13 | .text : { 14 | *(.text.entry) 15 | *(.text .text.*) 16 | } 17 | 18 | rodata_start = .; 19 | 20 | .rodata : { 21 | *(.rodata .rodata.*) 22 | } 23 | 24 | data_start = .; 25 | 26 | .data : { 27 | *(.data .data.*) 28 | } 29 | 30 | bss_start = .; 31 | 32 | .bss : { 33 | *(.snss .bss .bss.*) 34 | } 35 | 36 | kernel_end = .; 37 | } 38 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter15-scope/scope_17.rs: -------------------------------------------------------------------------------- 1 | static NUM: i32 = 18; 2 | 3 | fn coerce_static<'a>(_: &'a i32) -> &'a i32 { 4 | &NUM 5 | } 6 | 7 | fn main() { 8 | { 9 | let static_string = "I'm in read-only memory"; 10 | println!("static_string: {}", static_string); 11 | } 12 | 13 | { 14 | let lifetime_num = 9; 15 | 16 | let coerced_static = coerce_static(&lifetime_num); 17 | 18 | println!("coerced_static: {}", coerced_static); 19 | } 20 | 21 | println!("NUM: {} stays accessible!", NUM); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter10-mod/mod_3.rs: -------------------------------------------------------------------------------- 1 | use deeply::nested::function as other_function; 2 | 3 | fn function() { 4 | println!("called `function()`"); 5 | } 6 | 7 | mod deeply { 8 | pub mod nested { 9 | pub fn function() { 10 | println!("called `deeply::nested::function()`") 11 | } 12 | } 13 | } 14 | 15 | fn main() { 16 | other_function(); 17 | println!("Entering block"); 18 | { 19 | use deeply::nested::function; 20 | function(); 21 | println!("Leaving block"); 22 | } 23 | function(); 24 | } 25 | 26 | -------------------------------------------------------------------------------- /02-modular-programing/07-static_hashmap-2015/src/static_func/static_kv.rs: -------------------------------------------------------------------------------- 1 | // src/static_func/static_kv.rs 2 | use std::collections::HashMap; 3 | use std::sync::RwLock; 4 | pub const NF: &'static str = "not found"; 5 | lazy_static! { 6 | pub static ref MAP: HashMap = { 7 | let mut m = HashMap::new(); 8 | m.insert(0, "foo"); 9 | m 10 | }; 11 | pub static ref MAP_MUT: RwLock> = { 12 | let mut m = HashMap::new(); 13 | m.insert(0, "bar"); 14 | RwLock::new(m) 15 | }; 16 | } 17 | 18 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/strings/strings2.rs: -------------------------------------------------------------------------------- 1 | // strings2.rs 2 | // Make me compile without changing the function signature! 3 | // Execute `rustlings hint strings2` for hints :) 4 | 5 | fn main() { 6 | let word = String::from("green"); // Try not changing this line :) 7 | if is_a_color_word(&word) { 8 | println!("That is a color word I know!"); 9 | } else { 10 | println!("That is not a color word I know."); 11 | } 12 | } 13 | 14 | fn is_a_color_word(attempt: &str) -> bool { 15 | attempt == "green" || attempt == "blue" || attempt == "red" 16 | } 17 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter04-variable_bindings/variable_bindings_3.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let long_lived_binding = 1; 3 | 4 | { 5 | let short_lived_binding = 2; 6 | println!("inner short: {}", short_lived_binding); 7 | 8 | let long_lived_binding = 5_f32; 9 | println!("inner long: {}", long_lived_binding); 10 | } 11 | 12 | // println!("outer short: {}", short_lived_binding); 13 | println!("outer long: {}", long_lived_binding); 14 | 15 | let long_lived_binding = 'a'; 16 | println!("outer long: {}", long_lived_binding); 17 | } 18 | -------------------------------------------------------------------------------- /04-learn-rust-the-hard-way/ex08.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let formatter = "{:?} {:?} {:?} {:?}"; 3 | 4 | println!("{:?} {:?} {:?} {:?}", 1, 2, 3, 4); 5 | println!("{:?} {:?} {:?} {:?}", "one", "two", "three", "four"); 6 | println!("{:?} {:?} {:?} {:?}", true, false, false, true); 7 | println!("{:?} {:?} {:?} {:?}", formatter, formatter, formatter, formatter); 8 | println!("{:?} {:?} {:?} {:?}", 9 | "I had this thing.", 10 | "That you could type up right.", 11 | "But it didn't sing", 12 | "So I said goodnight."); 13 | } 14 | -------------------------------------------------------------------------------- /05-leetcode/prob09.rs: -------------------------------------------------------------------------------- 1 | struct Solution {} 2 | 3 | impl Solution { 4 | pub fn is_palindrome(x: i32) -> bool { 5 | if x < 0 { return false; } 6 | let mut num = x; 7 | let mut res: i32 = 0; 8 | loop { 9 | res = res * 10 + num % 10; 10 | num /= 10; 11 | if num == 0 { break; } 12 | } 13 | res == x 14 | } 15 | } 16 | 17 | fn main() { 18 | println!("{}", Solution::is_palindrome(121)); 19 | println!("{}", Solution::is_palindrome(-121)); 20 | println!("{}", Solution::is_palindrome(10)); 21 | } 22 | -------------------------------------------------------------------------------- /02-modular-programing/07-static_hashmap-2015/src/main.rs: -------------------------------------------------------------------------------- 1 | // src/main.rs 2 | #[macro_use] 3 | extern crate lazy_static; 4 | mod static_func; 5 | use static_func::static_kv; 6 | use static_func::read_func::{read_kv, rw_mut_kv}; 7 | 8 | fn main() { 9 | read_kv(); 10 | match rw_mut_kv() { 11 | Ok(()) => { 12 | let m = static_kv::MAP_MUT 13 | .read().map_err(|e| e.to_string()).unwrap(); 14 | assert_eq!("baz", *m.get(&1).unwrap_or(&static_kv::NF)); 15 | }, 16 | Err(e) => { println!("Error {}", e) } 17 | } 18 | } 19 | 20 | -------------------------------------------------------------------------------- /04-learn-rust-the-hard-way/ex12.rs: -------------------------------------------------------------------------------- 1 | fn readln(s: &mut String) { 2 | std::io::stdin().read_line(s).unwrap(); 3 | *s = s.trim().to_string(); 4 | } 5 | 6 | fn main(){ 7 | println!("How old are you?"); 8 | let mut age = String::new(); 9 | readln(&mut age); 10 | println!("How tall are you?"); 11 | let mut height = String::new(); 12 | readln(&mut height); 13 | println!("How much do you weigh?"); 14 | let mut weight = String::new(); 15 | readln(&mut weight); 16 | 17 | println!("So, you're {} old, {} tall and {} heavy.", age, height, weight); 18 | } 19 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter14-generics/generics_07.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::{Debug, Display}; 2 | 3 | fn compare_prints(t: &T) { 4 | println!("Debug: `{:?}`", t); 5 | println!("Display: `{}`", t); 6 | } 7 | 8 | fn compare_types(t: &T, u: &U) { 9 | println!("t: `{:?}`", t); 10 | println!("u: `{:?}`", u); 11 | } 12 | 13 | fn main() { 14 | let string = "words"; 15 | let array = [1, 2, 3]; 16 | let vec = vec![1, 2, 3]; 17 | 18 | compare_prints(&string); 19 | // compare_prints(&array); 20 | compare_types(&array, &vec); 21 | } 22 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter06-conversion/conversion_3.rs: -------------------------------------------------------------------------------- 1 | use std::string::ToString; 2 | 3 | struct Circle { 4 | radius: i32 5 | } 6 | 7 | impl ToString for Circle { 8 | fn to_string(&self) -> String { 9 | format!("Circle of radius {:?}", self.radius) 10 | } 11 | } 12 | 13 | fn main() { 14 | let circle = Circle { radius: 6 }; 15 | println!("{}", circle.to_string()); 16 | 17 | let parsed: i32 = "5".parse().unwrap(); 18 | let turbo_parsed = "10".parse::().unwrap(); 19 | 20 | let sum = parsed + turbo_parsed; 21 | println!{"Sum: {:?}", sum}; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/conversions/using_as.rs: -------------------------------------------------------------------------------- 1 | // Type casting in Rust is done via the usage of the `as` operator. 2 | // Please note that the `as` operator is not only used when type casting. 3 | // It also helps with renaming imports. 4 | 5 | // The goal is to make sure that the division does not fail to compile 6 | fn average(values: &[f64]) -> f64 { 7 | let total = values 8 | .iter() 9 | .fold(0.0, |a, b| a + b); 10 | total / (values.len() as f64) 11 | } 12 | 13 | fn main() { 14 | let values = [3.5, 0.3, 13.0, 11.7]; 15 | println!("{}", average(&values)); 16 | } 17 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4/os/src/algorithm/src/allocator/mod.rs: -------------------------------------------------------------------------------- 1 | //! 负责分配 / 回收的数据结构 2 | 3 | mod segment_tree_allocator; 4 | mod stacked_allocator; 5 | 6 | /// 分配器:固定容量,每次分配 / 回收一个元素 7 | pub trait Allocator { 8 | /// 给定容量,创建分配器 9 | fn new(capacity: usize) -> Self; 10 | /// 分配一个元素,无法分配则返回 `None` 11 | fn alloc(&mut self) -> Option; 12 | /// 回收一个元素 13 | fn dealloc(&mut self, index: usize); 14 | } 15 | 16 | pub use segment_tree_allocator::SegmentTreeAllocator; 17 | pub use stacked_allocator::StackedAllocator; 18 | 19 | /// 默认使用的分配器 20 | pub type AllocatorImpl = StackedAllocator; 21 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab5/os/src/algorithm/src/allocator/mod.rs: -------------------------------------------------------------------------------- 1 | //! 负责分配 / 回收的数据结构 2 | 3 | mod segment_tree_allocator; 4 | mod stacked_allocator; 5 | 6 | /// 分配器:固定容量,每次分配 / 回收一个元素 7 | pub trait Allocator { 8 | /// 给定容量,创建分配器 9 | fn new(capacity: usize) -> Self; 10 | /// 分配一个元素,无法分配则返回 `None` 11 | fn alloc(&mut self) -> Option; 12 | /// 回收一个元素 13 | fn dealloc(&mut self, index: usize); 14 | } 15 | 16 | pub use segment_tree_allocator::SegmentTreeAllocator; 17 | pub use stacked_allocator::StackedAllocator; 18 | 19 | /// 默认使用的分配器 20 | pub type AllocatorImpl = StackedAllocator; 21 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/os/src/algorithm/src/allocator/mod.rs: -------------------------------------------------------------------------------- 1 | //! 负责分配 / 回收的数据结构 2 | 3 | mod segment_tree_allocator; 4 | mod stacked_allocator; 5 | 6 | /// 分配器:固定容量,每次分配 / 回收一个元素 7 | pub trait Allocator { 8 | /// 给定容量,创建分配器 9 | fn new(capacity: usize) -> Self; 10 | /// 分配一个元素,无法分配则返回 `None` 11 | fn alloc(&mut self) -> Option; 12 | /// 回收一个元素 13 | fn dealloc(&mut self, index: usize); 14 | } 15 | 16 | pub use segment_tree_allocator::SegmentTreeAllocator; 17 | pub use stacked_allocator::StackedAllocator; 18 | 19 | /// 默认使用的分配器 20 | pub type AllocatorImpl = StackedAllocator; 21 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/user/src/bin/test_openfile.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | #[macro_use] 5 | extern crate user_lib; 6 | extern crate alloc; 7 | 8 | use alloc::string::String; 9 | 10 | #[no_mangle] 11 | pub fn main() -> usize { 12 | let fd = user_lib::sys_open("hello", 0, 0) as usize; 13 | let mut buffer: [u8; 32] = [0; 32]; 14 | user_lib::sys_read(fd, &mut buffer); 15 | let mut res = String::new(); 16 | for ch in buffer.iter() { 17 | res.push(*ch as char); 18 | } 19 | println!("{}", res); 20 | user_lib::sys_close(fd); 21 | 0 22 | } 23 | 24 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/user/src/bin/test_openfile.rs: -------------------------------------------------------------------------------- 1 | #![no_std] 2 | #![no_main] 3 | 4 | #[macro_use] 5 | extern crate user_lib; 6 | extern crate alloc; 7 | 8 | use alloc::string::String; 9 | 10 | #[no_mangle] 11 | pub fn main() -> usize { 12 | let fd = user_lib::sys_open("hello", 0, 0) as usize; 13 | let mut buffer: [u8; 32] = [0; 32]; 14 | user_lib::sys_read(fd, &mut buffer); 15 | let mut res = String::new(); 16 | for ch in buffer.iter() { 17 | res.push(*ch as char); 18 | } 19 | println!("{}", res); 20 | user_lib::sys_close(fd); 21 | 0 22 | } 23 | 24 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter09-fn/fn_10.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let vec1 = vec![1, 2, 3]; 3 | let vec2 = vec![4, 5, 6]; 4 | 5 | let mut iter = vec1.iter(); 6 | let mut into_iter = vec2.into_iter(); 7 | 8 | println!("Find 2 in vec1: {:?}", iter .find(|&&x| x == 2)); 9 | println!("Find 2 in vec2: {:?}", into_iter.find(| &x| x == 2)); 10 | 11 | let array1 = [1, 2, 3]; 12 | let array2 = [4, 5, 6]; 13 | 14 | println!("Find 2 in array1: {:?}", array1.iter() .find(|&&x| x == 2)); 15 | println!("Find 2 in array2: {:?}", array2.into_iter().find(|&&x| x == 2)); 16 | } 17 | 18 | -------------------------------------------------------------------------------- /02-modular-programing/08-static_hashmap-2018/src/read_func/static_kv.rs: -------------------------------------------------------------------------------- 1 | // src/read_func/static_kv.rs 2 | use lazy_static::lazy_static; 3 | use std::collections::HashMap; 4 | use std::sync::RwLock; 5 | pub const NF: &'static str = "not found"; 6 | lazy_static! { 7 | pub static ref MAP: HashMap = { 8 | let mut m = HashMap::new(); 9 | m.insert(0, "foo"); 10 | m 11 | }; 12 | pub static ref MAP_MUT: RwLock> = { 13 | let mut m = HashMap::new(); 14 | m.insert(0, "bar"); 15 | RwLock::new(m) 16 | }; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter06-conversion/conversion_1.rs: -------------------------------------------------------------------------------- 1 | use std::convert::From; 2 | 3 | #[derive(Debug)] 4 | struct Number { 5 | value: i32, 6 | } 7 | 8 | impl From for Number { 9 | fn from(item: i32) -> Self { 10 | Number { value: item } 11 | } 12 | } 13 | 14 | fn main() { 15 | let my_str = "hello"; 16 | let my_string = String::from(my_str); 17 | println!("{}", my_string); 18 | 19 | let num = Number::from(30); 20 | println!("My number is {:?}", num); 21 | 22 | let int = 5; 23 | let num: Number = int.into(); 24 | println!("My number is {:?}", num); 25 | } 26 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/os/src/algorithm/src/allocator/mod.rs: -------------------------------------------------------------------------------- 1 | //! 负责分配 / 回收的数据结构 2 | 3 | mod segment_tree_allocator; 4 | mod stacked_allocator; 5 | 6 | /// 分配器:固定容量,每次分配 / 回收一个元素 7 | pub trait Allocator { 8 | /// 给定容量,创建分配器 9 | fn new(capacity: usize) -> Self; 10 | /// 分配一个元素,无法分配则返回 `None` 11 | fn alloc(&mut self) -> Option; 12 | /// 回收一个元素 13 | fn dealloc(&mut self, index: usize); 14 | } 15 | 16 | pub use segment_tree_allocator::SegmentTreeAllocator; 17 | pub use stacked_allocator::StackedAllocator; 18 | 19 | /// 默认使用的分配器 20 | pub type AllocatorImpl = StackedAllocator; 21 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/os/src/memory/mapping/mod.rs: -------------------------------------------------------------------------------- 1 | //! 内存映射 2 | //! 3 | //! 每个线程保存一个 [`Mapping`],其中记录了所有的字段 [`Segment`]。 4 | //! 同时,也要追踪为页表或字段分配的所有物理页,目的是 drop 掉之后可以安全释放所有资源。 5 | 6 | #[allow(clippy::module_inception)] 7 | mod mapping; 8 | mod memory_set; 9 | mod page_table; 10 | mod page_table_entry; 11 | mod segment; 12 | mod swapper; 13 | 14 | pub use mapping::Mapping; 15 | pub use memory_set::MemorySet; 16 | pub use page_table::{PageTable, PageTableTracker}; 17 | pub use page_table_entry::{Flags, PageTableEntry}; 18 | pub use segment::{MapType, Segment}; 19 | pub use swapper::{Swapper, SwapperImpl}; 20 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_1/os/src/algorithm/src/allocator/mod.rs: -------------------------------------------------------------------------------- 1 | //! 负责分配 / 回收的数据结构 2 | 3 | mod segment_tree_allocator; 4 | mod stacked_allocator; 5 | 6 | /// 分配器:固定容量,每次分配 / 回收一个元素 7 | pub trait Allocator { 8 | /// 给定容量,创建分配器 9 | fn new(capacity: usize) -> Self; 10 | /// 分配一个元素,无法分配则返回 `None` 11 | fn alloc(&mut self) -> Option; 12 | /// 回收一个元素 13 | fn dealloc(&mut self, index: usize); 14 | } 15 | 16 | pub use segment_tree_allocator::SegmentTreeAllocator; 17 | pub use stacked_allocator::StackedAllocator; 18 | 19 | /// 默认使用的分配器 20 | pub type AllocatorImpl = StackedAllocator; 21 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_2/os/src/algorithm/src/allocator/mod.rs: -------------------------------------------------------------------------------- 1 | //! 负责分配 / 回收的数据结构 2 | 3 | mod segment_tree_allocator; 4 | mod stacked_allocator; 5 | 6 | /// 分配器:固定容量,每次分配 / 回收一个元素 7 | pub trait Allocator { 8 | /// 给定容量,创建分配器 9 | fn new(capacity: usize) -> Self; 10 | /// 分配一个元素,无法分配则返回 `None` 11 | fn alloc(&mut self) -> Option; 12 | /// 回收一个元素 13 | fn dealloc(&mut self, index: usize); 14 | } 15 | 16 | pub use segment_tree_allocator::SegmentTreeAllocator; 17 | pub use stacked_allocator::StackedAllocator; 18 | 19 | /// 默认使用的分配器 20 | pub type AllocatorImpl = StackedAllocator; 21 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/os/src/algorithm/src/allocator/mod.rs: -------------------------------------------------------------------------------- 1 | //! 负责分配 / 回收的数据结构 2 | 3 | mod segment_tree_allocator; 4 | mod stacked_allocator; 5 | 6 | /// 分配器:固定容量,每次分配 / 回收一个元素 7 | pub trait Allocator { 8 | /// 给定容量,创建分配器 9 | fn new(capacity: usize) -> Self; 10 | /// 分配一个元素,无法分配则返回 `None` 11 | fn alloc(&mut self) -> Option; 12 | /// 回收一个元素 13 | fn dealloc(&mut self, index: usize); 14 | } 15 | 16 | pub use segment_tree_allocator::SegmentTreeAllocator; 17 | pub use stacked_allocator::StackedAllocator; 18 | 19 | /// 默认使用的分配器 20 | pub type AllocatorImpl = StackedAllocator; 21 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/os/src/algorithm/src/allocator/mod.rs: -------------------------------------------------------------------------------- 1 | //! 负责分配 / 回收的数据结构 2 | 3 | mod segment_tree_allocator; 4 | mod stacked_allocator; 5 | 6 | /// 分配器:固定容量,每次分配 / 回收一个元素 7 | pub trait Allocator { 8 | /// 给定容量,创建分配器 9 | fn new(capacity: usize) -> Self; 10 | /// 分配一个元素,无法分配则返回 `None` 11 | fn alloc(&mut self) -> Option; 12 | /// 回收一个元素 13 | fn dealloc(&mut self, index: usize); 14 | } 15 | 16 | pub use segment_tree_allocator::SegmentTreeAllocator; 17 | pub use stacked_allocator::StackedAllocator; 18 | 19 | /// 默认使用的分配器 20 | pub type AllocatorImpl = StackedAllocator; 21 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter01-hello/hello_5.rs: -------------------------------------------------------------------------------- 1 | use std::fmt; 2 | 3 | struct List(Vec); 4 | 5 | impl fmt::Display for List { 6 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 7 | let vec = &self.0; 8 | 9 | write!(f, "[")?; 10 | 11 | for (count, v) in vec.iter().enumerate() { 12 | if count != 0 { write!(f, ", ")?; } 13 | // write!(f, "{}", v)?; 14 | write!(f, "{}: {}", count, v)?; 15 | } 16 | 17 | write!(f, "]") 18 | } 19 | } 20 | 21 | fn main() { 22 | let v = List(vec![1, 2, 3]); 23 | println!("{}", v); 24 | } 25 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter08-flow_control/flow_control_01.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let n = 5; 3 | 4 | if n < 0 { 5 | print!("{} is negative", n); 6 | } else if n > 0 { 7 | print!("{} is positive", n); 8 | } else { 9 | print!("{} is zero", n); 10 | } 11 | 12 | let big_n = 13 | if n < 10 && n > -10 { 14 | println!(", and is a small number, increase ten-fold"); 15 | 10 * n 16 | } else { 17 | println!(", and is a big number, half the number"); 18 | n / 2 19 | }; 20 | println!("{} -> {}", n, big_n); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /02-modular-programing/04-use_regex/src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate regex; 2 | use regex::Regex; 3 | 4 | fn main() { 5 | let re = Regex::new(r"(?x) 6 | (?P\d{4}) # the year 7 | - 8 | (?P\d{2}) # the month 9 | - 10 | (?P\d{2}) # the day 11 | ").unwrap(); 12 | let caps = re.captures("2018-01-01").unwrap(); 13 | assert_eq!("2018", &caps["year"]); 14 | assert_eq!("01", &caps["month"]); 15 | assert_eq!("01", &caps["day"]); 16 | let after = re.replace_all("2018-01-01", "$month/$day/$year"); 17 | assert_eq!(after, "01/01/2018"); 18 | } 19 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter03-custom_types/custom_types_3.rs: -------------------------------------------------------------------------------- 1 | enum Status { 2 | Rich, 3 | Poor, 4 | } 5 | 6 | enum Work { 7 | Civilian, 8 | Soldier, 9 | } 10 | 11 | fn main() { 12 | use Status::{Poor, Rich}; 13 | use Work::*; 14 | 15 | let status = Poor; 16 | let work = Civilian; 17 | 18 | match status { 19 | Rich => println!("The rich have lots of money!"), 20 | Poor => println!("The poor have no money..."), 21 | } 22 | 23 | match work { 24 | Civilian => println!("Civilians work!"), 25 | Soldier => println!("Soldiers fight!"), 26 | } 27 | } 28 | 29 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter14-generics/generics_06.rs: -------------------------------------------------------------------------------- 1 | struct Cardinal; 2 | struct BlueJay; 3 | struct Turkey; 4 | 5 | trait Red {} 6 | trait Blue {} 7 | 8 | impl Red for Cardinal {} 9 | impl Blue for BlueJay {} 10 | 11 | fn red(_: &T) -> &'static str { "red" } 12 | fn blue(_: &T) -> &'static str { "blue" } 13 | 14 | fn main() { 15 | let cardinal = Cardinal; 16 | let blue_jay = BlueJay; 17 | let _turkey = Turkey; 18 | 19 | println!("A cardinal is {}", red(&cardinal)); 20 | println!("A blue jay is {}", blue(&blue_jay)); 21 | // println!("A turkey is {}", red(&_turkey)); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/test4.rs: -------------------------------------------------------------------------------- 1 | // test4.rs 2 | // This test covers the sections: 3 | // - Modules 4 | // - Macros 5 | 6 | // Write a macro that passes the test! No hints this time, you can do it! 7 | 8 | macro_rules! my_macro { 9 | ($str: expr) => (String::from("Hello ") + $str) 10 | } 11 | 12 | #[cfg(test)] 13 | mod tests { 14 | use super::*; 15 | 16 | #[test] 17 | fn test_my_macro_world() { 18 | assert_eq!(my_macro!("world!"), "Hello world!"); 19 | } 20 | 21 | #[test] 22 | fn test_my_macro_goodbye() { 23 | assert_eq!(my_macro!("goodbye!"), "Hello goodbye!"); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/move_semantics/move_semantics1.rs: -------------------------------------------------------------------------------- 1 | // move_semantics1.rs 2 | // Make me compile! Execute `rustlings hint move_semantics1` for hints :) 3 | 4 | fn main() { 5 | let vec0 = Vec::new(); 6 | 7 | let mut vec1 = fill_vec(vec0); 8 | 9 | println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); 10 | 11 | vec1.push(88); 12 | 13 | println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); 14 | } 15 | 16 | fn fill_vec(vec: Vec) -> Vec { 17 | let mut vec = vec; 18 | 19 | vec.push(22); 20 | vec.push(44); 21 | vec.push(66); 22 | 23 | vec 24 | } 25 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter15-scope/scope_04.rs: -------------------------------------------------------------------------------- 1 | fn eat_box_i32(boxed_i32: Box) { 2 | println!("Destroying box that contains {}", boxed_i32); 3 | } 4 | 5 | fn borrow_i32(borrowed_i32: &i32) { 6 | println!("This int is: {}", borrowed_i32); 7 | } 8 | 9 | fn main() { 10 | let boxed_i32 = Box::new(5_i32); 11 | let stacked_i32 = 6_i32; 12 | 13 | borrow_i32(&boxed_i32); 14 | borrow_i32(&stacked_i32); 15 | 16 | { 17 | let _ref_to_i32: &i32 = &boxed_i32; 18 | 19 | // eat_box_i32(boxed_i32); 20 | 21 | borrow_i32(_ref_to_i32); 22 | } 23 | 24 | eat_box_i32(boxed_i32); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter17-macro/macro_3.rs: -------------------------------------------------------------------------------- 1 | macro_rules! test { 2 | ($left: expr; and $right: expr) => ( 3 | println!("{:?} and {:?} is {:?}", 4 | stringify!($left), 5 | stringify!($right), 6 | $left && $right) 7 | ); 8 | 9 | ($left: expr; or $right: expr) => ( 10 | println!("{:?} or {:?} is {:?}", 11 | stringify!($left), 12 | stringify!($right), 13 | $left || $right) 14 | ); 15 | } 16 | 17 | fn main() { 18 | test!(1i32 + 1 == 2i32; and 2i32 * 2 == 4i32); 19 | test!(true; or false); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /02-modular-programing/10-csv_challenge/benches/file_op_bench.rs: -------------------------------------------------------------------------------- 1 | #![feature(test)] 2 | extern crate test; 3 | use test::Bencher; 4 | use std::path::PathBuf; 5 | use csv_challenge::load_csv; 6 | /* 7 | use csv_challenge::{ 8 | Opt, 9 | {load_csv, write_csv}, 10 | replace_column, 11 | }; 12 | */ 13 | 14 | #[bench] 15 | fn bench_read_100times(b: &mut Bencher) { 16 | b.iter(|| { 17 | let n = test::black_box(100); 18 | (0..n).fold(0, |_, _| { test_load_csv(); 0 }) 19 | }); 20 | } 21 | 22 | fn test_load_csv() { 23 | let filename = PathBuf::from("./input/challenge.csv"); 24 | load_csv(filename); 25 | } 26 | 27 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter18-error/error_09.rs: -------------------------------------------------------------------------------- 1 | use std::num::ParseIntError; 2 | 3 | fn multiply(first_number_str: &str, second_number_str: &str) -> Result { 4 | let first_number = first_number_str.parse::()?; 5 | let second_number = second_number_str.parse::()?; 6 | 7 | Ok(first_number * second_number) 8 | } 9 | 10 | fn print(result: Result) { 11 | match result { 12 | Ok(n) => println!("n is {}", n), 13 | Err(e) => println!("Error: {}", e), 14 | } 15 | } 16 | 17 | fn main() { 18 | print(multiply("10", "2")); 19 | print(multiply("t", "2")); 20 | } 21 | 22 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/functions/functions4.rs: -------------------------------------------------------------------------------- 1 | // functions4.rs 2 | // Make me compile! Execute `rustlings hint functions4` for hints :) 3 | 4 | // This store is having a sale where if the price is an even number, you get 5 | // 10 Rustbucks off, but if it's an odd number, it's 3 Rustbucks off. 6 | 7 | fn main() { 8 | let original_price = 51; 9 | println!("Your sale price is {}", sale_price(original_price)); 10 | } 11 | 12 | fn sale_price(price: i32) -> i32 { 13 | if is_even(price) { 14 | price - 10 15 | } else { 16 | price - 3 17 | } 18 | } 19 | 20 | fn is_even(num: i32) -> bool { 21 | num % 2 == 0 22 | } 23 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/option/option1.rs: -------------------------------------------------------------------------------- 1 | // option1.rs 2 | // Make me compile! Execute `rustlings hint option1` for hints 3 | 4 | // you can modify anything EXCEPT for this function's sig 5 | fn print_number(maybe_number: Option) { 6 | println!("printing: {}", maybe_number.unwrap()); 7 | } 8 | 9 | fn main() { 10 | print_number(Some(13)); 11 | print_number(Some(99)); 12 | 13 | let mut numbers: [Option; 5] = [None; 5]; 14 | for iter in 0..5 { 15 | let number_to_add: u16 = { 16 | ((iter * 5) + 2) / (4 * 16) 17 | }; 18 | 19 | numbers[iter as usize] = Some(number_to_add); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter09-fn/fn_01.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | fizzbuzz_to(100); 3 | } 4 | 5 | fn is_divisible_by(lhs: u32, rhs: u32) -> bool { 6 | if rhs == 0 { 7 | return false; 8 | } 9 | 10 | lhs % rhs == 0 11 | } 12 | 13 | fn fizzbuzz(n: u32) -> () { 14 | if is_divisible_by(n, 15) { 15 | println!("fizzbuzz"); 16 | } else if is_divisible_by(n, 3) { 17 | println!("fizz"); 18 | } else if is_divisible_by(n, 5) { 19 | println!("buzz"); 20 | } else { 21 | println!("{}", n); 22 | } 23 | } 24 | 25 | fn fizzbuzz_to(n: u32) { 26 | for n in 1..n + 1 { 27 | fizzbuzz(n); 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/enums/README.md: -------------------------------------------------------------------------------- 1 | ### Enums 2 | 3 | Rust allows you to define types called "enums" which enumerate possible values. 4 | Enums are a feature in many languages, but their capabilities differ in each language. Rust’s enums are most similar to algebraic data types in functional languages, such as F#, OCaml, and Haskell. 5 | Useful in combination with enums is Rust's "pattern matching" facility, which makes it easy to run different code for different values of an enumeration. 6 | 7 | #### Book Sections 8 | 9 | - [Enums](https://doc.rust-lang.org/book/ch06-00-enums.html) 10 | - [Pattern syntax](https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html) 11 | -------------------------------------------------------------------------------- /04-learn-rust-the-hard-way/ex11.rs: -------------------------------------------------------------------------------- 1 | fn main(){ 2 | println!("How old are you?"); 3 | let mut age = String::new(); 4 | std::io::stdin().read_line(&mut age).unwrap(); 5 | age = age.trim().to_string(); 6 | println!("How tall are you?"); 7 | let mut height = String::new(); 8 | std::io::stdin().read_line(&mut height).unwrap(); 9 | height = height.trim().to_string(); 10 | println!("How much do you weigh?"); 11 | let mut weight = String::new(); 12 | std::io::stdin().read_line(&mut weight).unwrap(); 13 | weight = weight.trim().to_string(); 14 | 15 | println!("So, you're {} old, {} tall and {} heavy.", age, height, weight); 16 | } 17 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter02-primitives/primitive_2.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("1 + 2 = {}", 1u32 + 2); 3 | println!("1 - 2 = {}", 1i32 - 2); 4 | 5 | println!("true AND false is {}", true && false); 6 | println!("true OR false is {}", true || false); 7 | println!("NOT true is {}", !true); 8 | 9 | println!("0011 AND 0101 is {:04b}", 0b0011u32 & 0b0101); 10 | println!("0011 OR 0101 is {:04b}", 0b0011u32 | 0b0101); 11 | println!("0011 XOR 0101 is {:04b}", 0b0011u32 ^ 0b0101); 12 | println!("1 << 5 is {}", 1u32 << 5); 13 | println!("0x80 >> 2 is 0x{:x}", 0x80u32 >> 2); 14 | 15 | println!("One million is written as {}", 1_000_000u32); 16 | } 17 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter14-generics/generics_09.rs: -------------------------------------------------------------------------------- 1 | struct Years(i64); 2 | 3 | struct Days(i64); 4 | 5 | impl Years { 6 | pub fn to_days(&self) -> Days { 7 | Days(self.0 * 365) 8 | } 9 | } 10 | 11 | 12 | impl Days { 13 | pub fn to_years(&self) -> Years { 14 | Years(self.0 / 365) 15 | } 16 | } 17 | 18 | fn old_enough(age: &Years) -> bool { 19 | age.0 >= 18 20 | } 21 | 22 | fn main() { 23 | let age = Years(5); 24 | let age_days = age.to_days(); 25 | println!("Old enough {}", old_enough(&age)); 26 | println!("Old enough {}", old_enough(&age_days.to_years())); 27 | // println!("Old enough {}", old_enough(&age_days)); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter18-error/error_11.rs: -------------------------------------------------------------------------------- 1 | use std::num::ParseIntError; 2 | 3 | fn double_first(vec: Vec<&str>) -> Result, ParseIntError> { 4 | let opt = vec.first().map(|first| { 5 | first.parse::().map(|n| 2 * n) 6 | }); 7 | 8 | opt.map_or(Ok(None), |r| r.map(Some)) 9 | } 10 | 11 | fn main() { 12 | let numbers = vec!["42", "93", "18"]; 13 | let empty = vec![]; 14 | let strings = vec!["tofu", "93", "18"]; 15 | 16 | println!("The first doubled is {:?}", double_first(numbers)); 17 | println!("The first doubled is {:?}", double_first(empty)); 18 | println!("The first doubled is {:?}", double_first(strings)); 19 | } 20 | 21 | -------------------------------------------------------------------------------- /05-leetcode/prob07.rs: -------------------------------------------------------------------------------- 1 | struct Solution {} 2 | 3 | impl Solution { 4 | pub fn reverse(x: i32) -> i32 { 5 | let mut num = x; 6 | let mut res: i32 = 0; 7 | loop { 8 | res = match res.checked_mul(10) { 9 | Some(n) => n + num % 10, 10 | None => return 0, 11 | }; 12 | num /= 10; 13 | if num == 0 { break; } 14 | } 15 | res 16 | } 17 | } 18 | 19 | fn main() { 20 | println!("{}", Solution::reverse(123)); 21 | println!("{}", Solution::reverse(-123)); 22 | println!("{}", Solution::reverse(120)); 23 | println!("{}", Solution::reverse(2147483647)); 24 | } 25 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter18-error/error_07.rs: -------------------------------------------------------------------------------- 1 | use std::num::ParseIntError; 2 | 3 | type AliasedResult = Result; 4 | 5 | fn multiply(first_number_str: &str, second_number_str: &str) -> AliasedResult { 6 | first_number_str.parse::().and_then(|first_number| { 7 | second_number_str.parse::().map(|second_number| first_number * second_number) 8 | }) 9 | } 10 | 11 | fn print(result: AliasedResult) { 12 | match result { 13 | Ok(n) => println!("n is {}", n), 14 | Err(e) => println!("Error: {}", e), 15 | } 16 | } 17 | 18 | fn main() { 19 | print(multiply("10", "2")); 20 | print(multiply("t", "2")); 21 | } 22 | 23 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab2/os/src/memory/mod.rs: -------------------------------------------------------------------------------- 1 | //! 内存管理模块 2 | //! 3 | //! 负责空间分配和虚拟地址映射 4 | 5 | // 因为模块内包含许多基础设施类别,实现了许多以后可能会用到的函数, 6 | // 所以在模块范围内不提示「未使用的函数」等警告 7 | #![allow(dead_code)] 8 | 9 | pub mod address; 10 | pub mod config; 11 | pub mod frame; 12 | pub mod heap; 13 | pub mod range; 14 | 15 | /// 一个缩写,模块中一些函数会使用 16 | pub type MemoryResult = Result; 17 | 18 | pub use {address::*, config::*, frame::FRAME_ALLOCATOR, range::Range}; 19 | 20 | /// 初始化内存相关的子模块 21 | /// 22 | /// - [`heap::init`] 23 | pub fn init() { 24 | heap::init(); 25 | // 允许内核读写用户态内存 26 | unsafe { riscv::register::sstatus::set_sum() }; 27 | 28 | println!("mod memory initialized"); 29 | } 30 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab2/os/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "os" 3 | version = "0.1.0" 4 | authors = ["涂轶翔 ", "赵成钢 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | algorithm = { path = 'src/algorithm' } 11 | buddy_system_allocator = "0.3.9" 12 | lazy_static = { version = "1.4.0", features = ["spin_no_std"] } 13 | riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] } 14 | spin = "0.5.2" 15 | 16 | # panic 时直接终止,因为我们没有实现堆栈展开的功能 17 | [profile.dev] 18 | panic = "abort" 19 | [profile.release] 20 | panic = "abort" 21 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter18-error/error_06_2.rs: -------------------------------------------------------------------------------- 1 | use std::num::ParseIntError; 2 | 3 | fn multiply(first_number_str: &str, second_number_str: &str) -> Result { 4 | first_number_str.parse::().and_then(|first_number| { 5 | second_number_str.parse::().map(|second_number| first_number * second_number) 6 | }) 7 | } 8 | 9 | fn print(result: Result) { 10 | match result { 11 | Ok(n) => println!("n is {}", n), 12 | Err(e) => println!("Error: {}", e), 13 | } 14 | } 15 | 16 | fn main() { 17 | let twenty = multiply("10", "2"); 18 | print(twenty); 19 | 20 | let tt = multiply("t", "2"); 21 | print(tt); 22 | } 23 | 24 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/move_semantics/move_semantics2.rs: -------------------------------------------------------------------------------- 1 | // move_semantics2.rs 2 | // Make me compile without changing line 13! 3 | // Execute `rustlings hint move_semantics2` for hints :) 4 | 5 | fn main() { 6 | let vec0 = Vec::new(); 7 | 8 | // Do not change the following line! 9 | println!("{} has length {} content `{:?}`", "vec0", vec0.len(), vec0); 10 | 11 | let mut vec1 = fill_vec(vec0); 12 | 13 | vec1.push(88); 14 | 15 | println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); 16 | } 17 | 18 | fn fill_vec(vec: Vec) -> Vec { 19 | let mut vec = vec; 20 | 21 | vec.push(22); 22 | vec.push(44); 23 | vec.push(66); 24 | 25 | vec 26 | } 27 | -------------------------------------------------------------------------------- /04-learn-rust-the-hard-way/ex06.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let x = format!("There are {} types of people.", 10); 3 | let binary = "binary"; 4 | let do_not = "don't"; 5 | let y = format!("Those who know {} and those who {}.", binary, do_not); 6 | 7 | println!("{}", x); 8 | println!("{}", y); 9 | 10 | println!("I said: {:?}.", x); 11 | println!("I also said: '{}'.", y); 12 | 13 | let hilarious = false; 14 | let joke_evaluation = format!("Isn't that joke so funny?! {:?}", hilarious); 15 | 16 | println!("{}", joke_evaluation); 17 | 18 | let w = String::from("This is the left side of..."); 19 | let e = "a string with a right side."; 20 | println!("{}", w + e); 21 | } 22 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/move_semantics/move_semantics3.rs: -------------------------------------------------------------------------------- 1 | // move_semantics3.rs 2 | // Make me compile without adding new lines-- just changing existing lines! 3 | // (no lines with multiple semicolons necessary!) 4 | // Execute `rustlings hint move_semantics3` for hints :) 5 | 6 | fn main() { 7 | let vec0 = Vec::new(); 8 | 9 | let mut vec1 = fill_vec(vec0); 10 | 11 | println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); 12 | 13 | vec1.push(88); 14 | 15 | println!("{} has length {} content `{:?}`", "vec1", vec1.len(), vec1); 16 | } 17 | 18 | fn fill_vec(mut vec: Vec) -> Vec { 19 | vec.push(22); 20 | vec.push(44); 21 | vec.push(66); 22 | 23 | vec 24 | } 25 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/if/if1.rs: -------------------------------------------------------------------------------- 1 | // if1.rs 2 | 3 | pub fn bigger(a: i32, b: i32) -> i32 { 4 | if a > b { 5 | a 6 | } else { 7 | b 8 | } 9 | // Complete this function to return the bigger number! 10 | // Do not use: 11 | // - another function call 12 | // - additional variables 13 | // Execute `rustlings hint if1` for hints 14 | } 15 | 16 | // Don't mind this for now :) 17 | #[cfg(test)] 18 | mod tests { 19 | use super::*; 20 | 21 | #[test] 22 | fn ten_is_bigger_than_eight() { 23 | assert_eq!(10, bigger(10, 8)); 24 | } 25 | 26 | #[test] 27 | fn fortytwo_is_bigger_than_thirtytwo() { 28 | assert_eq!(42, bigger(32, 42)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /04-learn-rust-the-hard-way/ex07.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("Mary had a little lamb."); 3 | println!("Its fleece was white as {}", "snow"); 4 | println!("And everywhere that Mary went."); 5 | println!("{0}{0}{0}{0}{0}{0}{0}{0}{0}{0}", "."); 6 | 7 | let end1 = String::from("C"); 8 | let end2 = "h"; 9 | let end3 = "e"; 10 | let end4 = "e"; 11 | let end5 = "s"; 12 | let end6 = "e"; 13 | let end7 = String::from("B"); 14 | let end8 = "u"; 15 | let end9 = "r"; 16 | let end10 = "g"; 17 | let end11 = "e"; 18 | let end12 = "r"; 19 | 20 | println!("{}", end1 + end2 + end3 + end4 + end5 + end6); 21 | println!("{}", end7 + end8 + end9 + end10 + end11 + end12); 22 | } 23 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter01-hello/hello_3.rs: -------------------------------------------------------------------------------- 1 | #[derive(Debug)] 2 | struct Structure(i32); 3 | 4 | #[derive(Debug)] 5 | struct Deep(Structure); 6 | 7 | #[derive(Debug)] 8 | struct Person<'a> { 9 | name: &'a str, 10 | age: u8 11 | } 12 | 13 | fn main() { 14 | println!("{:?} months in a year.", 12); 15 | println!("{1:?} {0:?} is the {actor:?} name.", 16 | "Slater", 17 | "Christian", 18 | actor="actor's"); 19 | 20 | println!("Now {:?} will print!", Structure(3)); 21 | 22 | println!("Now {:?} will print!", Deep(Structure(7))); 23 | 24 | let name = "Peter"; 25 | let age = 27; 26 | let peter = Person { name, age }; 27 | 28 | println!("{:#?}", peter); 29 | } 30 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter09-fn/fn_05.rs: -------------------------------------------------------------------------------- 1 | fn apply(f: F) where F: FnOnce() { 2 | f(); 3 | } 4 | 5 | fn apply_to_3(f: F) -> i32 where F: Fn(i32) -> i32 { 6 | f(3) 7 | } 8 | 9 | fn main() { 10 | use std::mem; 11 | 12 | let greeting = "hello"; 13 | let mut farewell = "goodbye".to_owned(); 14 | 15 | let diary = || { 16 | println!("I said {}.", greeting); 17 | 18 | farewell.push_str("!!!"); 19 | println!("Then I screamed {}.", farewell); 20 | println!("Now I can sleep. zzzzz"); 21 | // 233333 22 | mem::drop(farewell); 23 | }; 24 | 25 | apply(diary); 26 | 27 | let double = |x| 2 * x; 28 | println!("3 doubled: {}", apply_to_3(double)); 29 | } 30 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/os/src/kernel/condvar.rs: -------------------------------------------------------------------------------- 1 | //! 条件变量 2 | 3 | use super::*; 4 | use alloc::collections::VecDeque; 5 | 6 | #[derive(Default)] 7 | pub struct Condvar { 8 | /// 所有等待此条件变量的线程 9 | watchers: Mutex>>, 10 | } 11 | 12 | impl Condvar { 13 | /// 令当前线程休眠,等待此条件变量 14 | pub fn wait(&self) { 15 | self.watchers 16 | .lock() 17 | .push_back(PROCESSOR.get().current_thread()); 18 | PROCESSOR.get().sleep_current_thread(); 19 | } 20 | 21 | /// 唤起一个等待此条件变量的线程 22 | pub fn notify_one(&self) { 23 | if let Some(thread) = self.watchers.lock().pop_front() { 24 | PROCESSOR.get().wake_thread(thread); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter17-macro/macro_2.rs: -------------------------------------------------------------------------------- 1 | macro_rules! create_function { 2 | ($func_name: ident) => ( 3 | fn $func_name() { 4 | println!("You called {:?}()", 5 | stringify!($func_name)) 6 | } 7 | ) 8 | } 9 | 10 | create_function!(foo); 11 | create_function!(bar); 12 | 13 | macro_rules! print_result { 14 | ($expression: expr) => ( 15 | println!("{:?} = {:?}", 16 | stringify!($expression), 17 | $expression) 18 | ) 19 | } 20 | 21 | fn main() { 22 | foo(); 23 | bar(); 24 | 25 | print_result!(1u32 + 1); 26 | 27 | print_result!({ 28 | let x = 1u32; 29 | x * x + 2 * x - 1 30 | }); 31 | } 32 | 33 | -------------------------------------------------------------------------------- /02-modular-programing/08-static_hashmap-2018/src/read_func.rs: -------------------------------------------------------------------------------- 1 | // src/read_func.rs 2 | pub mod static_kv; 3 | 4 | pub fn read_kv() { 5 | let ref m = static_kv::MAP; 6 | assert_eq!("foo", *m.get(&0).unwrap_or(&static_kv::NF)); 7 | assert_eq!(static_kv::NF, 8 | *m.get(&1).unwrap_or(&static_kv::NF)); 9 | } 10 | 11 | pub fn rw_mut_kv() -> Result<(), String> { 12 | { 13 | let m = static_kv::MAP_MUT 14 | .read().map_err(|e| e.to_string())?; 15 | assert_eq!("bar", *m.get(&0).unwrap_or(&static_kv::NF)); 16 | } 17 | { 18 | let mut m = static_kv::MAP_MUT 19 | .write().map_err(|e| e.to_string())?; 20 | m.insert(1, "baz"); 21 | } 22 | Ok(()) 23 | } 24 | 25 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter15-scope/scope_11.rs: -------------------------------------------------------------------------------- 1 | fn print_one<'a>(x: &'a i32) { 2 | println!("`print_one`: x is {}", x); 3 | } 4 | 5 | fn add_one<'a>(x: &'a mut i32) { 6 | *x += 1; 7 | } 8 | 9 | fn print_multi<'a, 'b>(x: &'a i32, y: &'b i32) { 10 | println!("`print_multi`: x is {}, y is {}", x, y); 11 | } 12 | 13 | fn pass_x<'a, 'b>(x: &'a i32, _: &'b i32) -> &'a i32 { x } 14 | 15 | // fn invalid_output<'a>() -> &'a String { &String::from("foo") } 16 | 17 | fn main() { 18 | let x = 7; 19 | let y = 9; 20 | 21 | print_one(&x); 22 | print_multi(&x, &y); 23 | 24 | let z = pass_x(&x, &y); 25 | print_one(z); 26 | 27 | let mut t = 3; 28 | add_one(&mut t); 29 | print_one(&t); 30 | } 31 | 32 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab1/os/src/interrupt/context.rs: -------------------------------------------------------------------------------- 1 | use core::fmt; 2 | use riscv::register::{sstatus::Sstatus, scause::Scause}; 3 | 4 | #[repr(C)] 5 | pub struct Context { 6 | pub x: [usize; 32], // 32 个通用寄存器 7 | pub sstatus: Sstatus, 8 | pub sepc: usize 9 | } 10 | 11 | /// 格式化输出 12 | /// 13 | /// # Example 14 | /// 15 | /// ```rust 16 | /// println!("{:x?}", Context); // {:x?} 表示用十六进制打印其中的数值 17 | /// ``` 18 | impl fmt::Debug for Context { 19 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 20 | f.debug_struct("Context") 21 | .field("registers", &self.x) 22 | .field("sstatus", &self.sstatus) 23 | .field("sepc", &self.sepc) 24 | .finish() 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/os/src/kernel/condvar.rs: -------------------------------------------------------------------------------- 1 | //! 条件变量 2 | 3 | use super::*; 4 | use alloc::collections::VecDeque; 5 | 6 | #[derive(Default)] 7 | pub struct Condvar { 8 | /// 所有等待此条件变量的线程 9 | watchers: Mutex>>, 10 | } 11 | 12 | impl Condvar { 13 | /// 令当前线程休眠,等待此条件变量 14 | pub fn wait(&self) { 15 | self.watchers 16 | .lock() 17 | .push_back(PROCESSOR.lock().current_thread()); 18 | PROCESSOR.lock().sleep_current_thread(); 19 | } 20 | 21 | /// 唤起一个等待此条件变量的线程 22 | pub fn notify_one(&self) { 23 | if let Some(thread) = self.watchers.lock().pop_front() { 24 | PROCESSOR.lock().wake_thread(thread); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_practice/os/src/kernel/condvar.rs: -------------------------------------------------------------------------------- 1 | //! 条件变量 2 | 3 | use super::*; 4 | use alloc::collections::VecDeque; 5 | 6 | #[derive(Default)] 7 | pub struct Condvar { 8 | /// 所有等待此条件变量的线程 9 | watchers: Mutex>>, 10 | } 11 | 12 | impl Condvar { 13 | /// 令当前线程休眠,等待此条件变量 14 | pub fn wait(&self) { 15 | self.watchers 16 | .lock() 17 | .push_back(PROCESSOR.lock().current_thread()); 18 | PROCESSOR.lock().sleep_current_thread(); 19 | } 20 | 21 | /// 唤起一个等待此条件变量的线程 22 | pub fn notify_one(&self) { 23 | if let Some(thread) = self.watchers.lock().pop_front() { 24 | PROCESSOR.lock().wake_thread(thread); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab2/os/src/entry.asm: -------------------------------------------------------------------------------- 1 | # 操作系统启动时所需的指令以及字段 2 | # 3 | # 我们在 linker.ld 中将程序入口设置为了 _start,因此在这里我们将填充这个标签 4 | # 它将会执行一些必要操作,然后跳转至我们用 rust 编写的入口函数 5 | # 6 | # 关于 RISC-V 下的汇编语言,可以参考 https://github.com/riscv/riscv-asm-manual/blob/master/riscv-asm.md 7 | # %hi 表示取 [12,32) 位,%lo 表示取 [0,12) 位 8 | 9 | .section .text.entry 10 | .globl _start 11 | # 目前 _start 的功能:将预留的栈空间写入 $sp,然后跳转至 rust_main 12 | _start: 13 | la sp, boot_stack_top 14 | jal rust_main 15 | 16 | # 回忆:bss 段是 ELF 文件中只记录长度,而全部初始化为 0 的一段内存空间 17 | # 这里声明字段 .bss.stack 作为操作系统启动时的栈 18 | .section .bss.stack 19 | .global boot_stack 20 | boot_stack: 21 | # 16K 启动栈大小 22 | .space 4096 * 16 23 | .global boot_stack_top 24 | boot_stack_top: 25 | # 栈结尾 -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6_challenge/os/src/kernel/condvar.rs: -------------------------------------------------------------------------------- 1 | //! 条件变量 2 | 3 | use super::*; 4 | use alloc::collections::VecDeque; 5 | 6 | #[derive(Default)] 7 | pub struct Condvar { 8 | /// 所有等待此条件变量的线程 9 | watchers: Mutex>>, 10 | } 11 | 12 | impl Condvar { 13 | /// 令当前线程休眠,等待此条件变量 14 | pub fn wait(&self) { 15 | self.watchers 16 | .lock() 17 | .push_back(PROCESSOR.lock().current_thread()); 18 | PROCESSOR.lock().sleep_current_thread(); 19 | } 20 | 21 | /// 唤起一个等待此条件变量的线程 22 | pub fn notify_one(&self) { 23 | if let Some(thread) = self.watchers.lock().pop_front() { 24 | PROCESSOR.lock().wake_thread(thread); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter16-trait/trait_6.rs: -------------------------------------------------------------------------------- 1 | #[derive(Debug, Clone, Copy)] 2 | struct Nil; 3 | 4 | #[derive(Clone, Debug)] 5 | struct Pair(Box, Box); 6 | 7 | fn main() { 8 | let nil = Nil; 9 | let copied_nil = nil; 10 | 11 | println!("original: {:?}", nil); 12 | println!("copy: {:?}", copied_nil); 13 | 14 | let pair = Pair(Box::new(1), Box::new(2)); 15 | println!("original: {:?}", pair); 16 | 17 | let moved_pair = pair; 18 | println!("copy: {:?}", moved_pair); 19 | 20 | // println!("original: {:?}", pair); 21 | 22 | let cloned_pair = moved_pair.clone(); 23 | drop(moved_pair); 24 | 25 | // println!("copy: {:?}", moved_pair); 26 | 27 | println!("clone: {:?}", cloned_pair); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/modules/modules2.rs: -------------------------------------------------------------------------------- 1 | // modules2.rs 2 | // Make me compile! Execute `rustlings hint modules2` for hints :) 3 | 4 | mod delicious_snacks { 5 | use self::fruits::PEAR as fruit; 6 | use self::veggies::CUCUMBER as veggie; 7 | 8 | pub mod fruits { 9 | pub const PEAR: &'static str = "Pear"; 10 | pub const APPLE: &'static str = "Apple"; 11 | } 12 | 13 | pub mod veggies { 14 | pub const CUCUMBER: &'static str = "Cucumber"; 15 | pub const CARROT: &'static str = "Carrot"; 16 | } 17 | } 18 | 19 | fn main() { 20 | println!( 21 | "favorite snacks: {} and {}", 22 | delicious_snacks::fruits::APPLE, 23 | delicious_snacks::veggies::CUCUMBER 24 | ); 25 | } 26 | -------------------------------------------------------------------------------- /05-leetcode/prob01.rs: -------------------------------------------------------------------------------- 1 | struct Solution {} 2 | 3 | impl Solution { 4 | pub fn two_sum(nums: Vec, target: i32) -> Vec { 5 | use std::collections::HashMap; 6 | let mut hashmap: HashMap = HashMap::new(); 7 | for i in 0..nums.len() { 8 | let n = nums[i]; 9 | let sub = target - n; 10 | if let Some(res) = hashmap.get(&sub) { 11 | return vec![*res, i as i32]; 12 | } 13 | hashmap.insert(n, i as i32); 14 | } 15 | Vec::::new() 16 | } 17 | } 18 | 19 | fn main() { 20 | let nums = vec![2, 7, 11, 15]; 21 | let target = 9; 22 | let res = Solution::two_sum(nums, target); 23 | println!("{:?}", res); 24 | } 25 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter08-flow_control/flow_control_10.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let reference = &4; 3 | match reference { 4 | &val => println!("Got a value via destructuring: {:?}", val), 5 | } 6 | 7 | match *reference { 8 | val => println!("Got a value via dereferencing: {:?}", val), 9 | } 10 | 11 | let _not_a_reference = 3; 12 | let ref _is_a_reference = 3; 13 | 14 | let value = 5; 15 | let mut mut_value = 6; 16 | 17 | match value { 18 | ref r => println!("Got a reference to a value: {:?}", *r), 19 | } 20 | 21 | match mut_value { 22 | ref mut m => { 23 | *m += 10; 24 | println!("We added 10. `mut_value`: {:?}", m); 25 | }, 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /02-modular-programing/07-static_hashmap-2015/src/static_func/read_func.rs: -------------------------------------------------------------------------------- 1 | // src/static_func/read_func.rs 2 | use static_kv; 3 | 4 | pub fn read_kv() { 5 | let ref m = static_kv::MAP; 6 | assert_eq!("foo", *m.get(&0).unwrap_or(&static_kv::NF)); 7 | assert_eq!(static_kv::NF, 8 | *m.get(&1).unwrap_or(&static_kv::NF)); 9 | } 10 | 11 | pub fn rw_mut_kv() -> Result<(), String> { 12 | { 13 | let m = static_kv::MAP_MUT 14 | .read().map_err(|e| e.to_string())?; 15 | assert_eq!("bar", *m.get(&0).unwrap_or(&static_kv::NF)); 16 | } 17 | { 18 | let mut m = static_kv::MAP_MUT 19 | .write().map_err(|e| e.to_string())?; 20 | m.insert(1, "baz"); 21 | } 22 | Ok(()) 23 | } 24 | 25 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter16-trait/trait_3.rs: -------------------------------------------------------------------------------- 1 | use std::ops; 2 | 3 | struct Foo; 4 | struct Bar; 5 | 6 | #[derive(Debug)] 7 | struct FooBar; 8 | 9 | #[derive(Debug)] 10 | struct BarFoo; 11 | 12 | impl ops::Add for Foo { 13 | type Output = FooBar; // why??? 14 | 15 | fn add(self, _rhs: Bar) -> FooBar { 16 | println!("> Foo.add(Bar) was called"); 17 | 18 | FooBar 19 | } 20 | } 21 | 22 | impl ops::Add for Bar { 23 | type Output = BarFoo; 24 | 25 | fn add(self, _rhs: Foo) -> BarFoo { 26 | println!("> Bar.add(Foo) was called"); 27 | 28 | BarFoo 29 | } 30 | } 31 | 32 | fn main() { 33 | println!("Foo + Bar = {:?}", Foo + Bar); 34 | println!("Bar + Foo = {:?}", Bar + Foo); 35 | } 36 | 37 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/enums/enums2.rs: -------------------------------------------------------------------------------- 1 | // enums2.rs 2 | // Make me compile! Execute `rustlings hint enums2` for hints! 3 | 4 | #[derive(Debug)] 5 | enum Message { 6 | // TODO: define the different variants used below 7 | Move { x: i32, y: i32 }, 8 | Echo (String), 9 | ChangeColor (i32, i32, i32), 10 | Quit 11 | } 12 | 13 | impl Message { 14 | fn call(&self) { 15 | println!("{:?}", &self); 16 | } 17 | } 18 | 19 | fn main() { 20 | let messages = [ 21 | Message::Move{ x: 10, y: 30 }, 22 | Message::Echo(String::from("hello world")), 23 | Message::ChangeColor(200, 255, 255), 24 | Message::Quit 25 | ]; 26 | 27 | for message in &messages { 28 | message.call(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter05-types/types_1.rs: -------------------------------------------------------------------------------- 1 | #![allow(overflowing_literals)] 2 | 3 | fn main() { 4 | let decimal = 65.4321_f32; 5 | // let integer: u8 = decimal; 6 | let integer = decimal as u8; 7 | let character = integer as char; 8 | println!("Casting: {} -> {} -> {}", decimal, integer, character); 9 | println!("1000 as a u16 is: {}", 1000 as u16); 10 | println!("1000 as a u8 is : {}", 1000 as u8); 11 | println!(" -1 as a u8 is : {}", (-1i8) as u8); 12 | println!("1000 mod 256 is : {}", 1000 % 256); 13 | 14 | println!(" 128 as a i16 is: {}", 128 as i16); 15 | println!(" 128 as a i8 is : {}", 128 as i8); 16 | println!("1000 as a u8 is : {}", 1000 as u8); 17 | println!(" 232 as a i8 is : {}", 232 as i8); 18 | } 19 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab0/os/src/panic.rs: -------------------------------------------------------------------------------- 1 | //! 代替 std 库,实现 panic 和 abort 的功能 2 | 3 | use core::panic::PanicInfo; 4 | use crate::sbi::shutdown; 5 | 6 | /// 打印 panic 的信息并 [`shutdown`] 7 | /// 8 | /// ### `#[panic_handler]` 属性 9 | /// 声明此函数是 panic 的回调 10 | #[panic_handler] 11 | fn panic_handler(info: &PanicInfo) -> ! { 12 | // `\x1b[??m` 是控制终端字符输出格式的指令,在支持的平台上可以改变文字颜色等等 13 | // 这里使用错误红 14 | // 需要全局开启 feature(panic_info_message) 才可以调用 .message() 函数 15 | // 参考:https://misc.flogisoft.com/bash/tip_colors_and_formatting 16 | println!("\x1b[1;31mpanic: '{}'\x1b[0m", info.message().unwrap()); 17 | shutdown() 18 | } 19 | 20 | /// 终止程序 21 | /// 22 | /// 调用 [`panic_handler`] 23 | #[no_mangle] 24 | extern "C" fn abort() -> ! { 25 | panic!("abort()") 26 | } 27 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab1/os/src/panic.rs: -------------------------------------------------------------------------------- 1 | //! 代替 std 库,实现 panic 和 abort 的功能 2 | 3 | use core::panic::PanicInfo; 4 | use crate::sbi::shutdown; 5 | 6 | /// 打印 panic 的信息并 [`shutdown`] 7 | /// 8 | /// ### `#[panic_handler]` 属性 9 | /// 声明此函数是 panic 的回调 10 | #[panic_handler] 11 | fn panic_handler(info: &PanicInfo) -> ! { 12 | // `\x1b[??m` 是控制终端字符输出格式的指令,在支持的平台上可以改变文字颜色等等 13 | // 这里使用错误红 14 | // 需要全局开启 feature(panic_info_message) 才可以调用 .message() 函数 15 | // 参考:https://misc.flogisoft.com/bash/tip_colors_and_formatting 16 | println!("\x1b[1;31mpanic: '{}'\x1b[0m", info.message().unwrap()); 17 | shutdown() 18 | } 19 | 20 | /// 终止程序 21 | /// 22 | /// 调用 [`panic_handler`] 23 | #[no_mangle] 24 | extern "C" fn abort() -> ! { 25 | panic!("abort()") 26 | } 27 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3/os/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "os" 3 | version = "0.1.0" 4 | authors = ["涂轶翔 ", "赵成钢 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | algorithm = { path = 'src/algorithm' } 11 | bitflags = "1.2.1" 12 | bit_field = "0.10.0" 13 | buddy_system_allocator = "0.3.9" 14 | lazy_static = { version = "1.4.0", features = ["spin_no_std"] } 15 | riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] } 16 | spin = "0.5.2" 17 | xmas-elf = "0.7.0" 18 | 19 | # panic 时直接终止,因为我们没有实现堆栈展开的功能 20 | [profile.dev] 21 | panic = "abort" 22 | [profile.release] 23 | panic = "abort" 24 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter15-scope/scope_13.rs: -------------------------------------------------------------------------------- 1 | #[derive(Debug)] 2 | struct Borrowed<'a>(&'a i32); 3 | 4 | #[derive(Debug)] 5 | struct NamedBorrowed<'a> { 6 | x: &'a i32, 7 | y: &'a i32, 8 | } 9 | 10 | #[derive(Debug)] 11 | enum Either<'a> { 12 | Num(i32), 13 | Ref(&'a i32), 14 | } 15 | 16 | fn main() { 17 | let x = 18; 18 | let y = 15; 19 | 20 | let single = Borrowed(&x); 21 | let double = NamedBorrowed { x: &x, y: &y }; 22 | let reference = Either::Ref(&x); 23 | let number = Either::Num(y); 24 | 25 | println!("x is borrowed in {:?}", single); 26 | println!("x and y are borrowed in {:?}", double); 27 | println!("x is borrowed in {:?}", reference); 28 | println!("y is *not* borrowed in {:?}", number); 29 | } 30 | 31 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter16-trait/trait_4.rs: -------------------------------------------------------------------------------- 1 | struct Droppable { 2 | name: &'static str, 3 | } 4 | 5 | impl Drop for Droppable { 6 | fn drop(&mut self) { 7 | println!("> Dropping {}", self.name); 8 | } 9 | } 10 | 11 | fn main() { 12 | let _a = Droppable { name: "a" }; 13 | 14 | { 15 | let _b = Droppable { name: "b" }; 16 | 17 | { 18 | let _c = Droppable { name: "c" }; 19 | let _d = Droppable { name: "d" }; 20 | 21 | println!("Exiting block B"); 22 | } 23 | println!("Just exited block B"); 24 | 25 | println!("Exiting block A"); 26 | } 27 | println!("Just exited block A"); 28 | 29 | drop(_a); 30 | 31 | println!("end of the main function"); 32 | } 33 | 34 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4/os/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "os" 3 | version = "0.1.0" 4 | authors = ["涂轶翔 ", "赵成钢 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | algorithm = { path = 'src/algorithm' } 11 | bit_field = "0.10.0" 12 | bitflags = "1.2.1" 13 | buddy_system_allocator = "0.3.9" 14 | hashbrown = "0.7.2" 15 | lazy_static = { version = "1.4.0", features = ["spin_no_std"] } 16 | riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] } 17 | spin = "0.5.2" 18 | 19 | # panic 时直接终止,因为我们没有实现堆栈展开的功能 20 | [profile.dev] 21 | panic = "abort" 22 | [profile.release] 23 | panic = "abort" 24 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/os/src/interrupt/mod.rs: -------------------------------------------------------------------------------- 1 | //! 中断模块 2 | //! 3 | //! 4 | 5 | mod context; 6 | mod handler; 7 | mod timer; 8 | 9 | use riscv::register::{sie, sstatus}; 10 | 11 | pub use context::Context; 12 | 13 | /// 初始化中断相关的子模块 14 | /// 15 | /// - [`handler::init`] 16 | /// - [`timer::init`] 17 | pub fn init() { 18 | handler::init(); 19 | timer::init(); 20 | println!("mod interrupt initialized"); 21 | } 22 | 23 | /// 等待一个外部中断 24 | /// 25 | /// 暂时开启中断(不包括时钟中断)并执行 `wfi` 指令 26 | /// 27 | /// 会在所有线程都在等待外部信号时调用 28 | pub fn wait_for_interrupt() { 29 | unsafe { 30 | sie::clear_stimer(); 31 | sstatus::set_sie(); 32 | llvm_asm!("wfi" :::: "volatile"); 33 | sstatus::clear_sie(); 34 | sie::set_stimer(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /04-learn-rust-the-hard-way/ex03.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("I will now count my chickens:"); 3 | 4 | println!("Hens {}", 25 + 30 / 6); 5 | println!("Roosters {}", 100 - 25 * 3 % 4); 6 | 7 | println!("Now I will count the eggs"); 8 | 9 | println!("{}", 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6); 10 | 11 | println!("Is it true that 3 + 2 < 5 - 7?"); 12 | 13 | println!("{}", 3 + 2 < 5 - 7); 14 | 15 | println!("What is 3 + 2? {}", 3 + 2); 16 | println!("What is 5 - 7? {}", 5 - 7); 17 | 18 | println!("Oh, that's why it's false"); 19 | 20 | println!("How about some more."); 21 | 22 | println!("Is it greater? {}", 5 > -2); 23 | println!("Is it greater or equal? {}", 5 >= -2); 24 | println!("Is it less or equal? {}", 5 <= -2); 25 | } 26 | -------------------------------------------------------------------------------- /04-learn-rust-the-hard-way/ex30.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let people = 30; 3 | let cars = 40; 4 | let buses = 15; 5 | 6 | if cars > people { 7 | println!("We should take the cars."); 8 | } else if cars < people { 9 | println!("We should not take the cars."); 10 | } else { 11 | println!("We can't decide."); 12 | } 13 | 14 | if buses > cars { 15 | println!("That's too many buses."); 16 | } else if buses < cars { 17 | println!("Maybe we could take the buses."); 18 | } else { 19 | println!("We still can't decide."); 20 | } 21 | 22 | if people > buses { 23 | println!("Alright, let's just take the buses."); 24 | } else { 25 | println!("Fine, let's stay home then."); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_1/os/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "os" 3 | version = "0.1.0" 4 | authors = ["涂轶翔 ", "赵成钢 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | algorithm = { path = 'src/algorithm' } 11 | bit_field = "0.10.0" 12 | bitflags = "1.2.1" 13 | buddy_system_allocator = "0.3.9" 14 | hashbrown = "0.7.2" 15 | lazy_static = { version = "1.4.0", features = ["spin_no_std"] } 16 | riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] } 17 | spin = "0.5.2" 18 | 19 | # panic 时直接终止,因为我们没有实现堆栈展开的功能 20 | [profile.dev] 21 | panic = "abort" 22 | [profile.release] 23 | panic = "abort" 24 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4_practice_2/os/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "os" 3 | version = "0.1.0" 4 | authors = ["涂轶翔 ", "赵成钢 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | algorithm = { path = 'src/algorithm' } 11 | bit_field = "0.10.0" 12 | bitflags = "1.2.1" 13 | buddy_system_allocator = "0.3.9" 14 | hashbrown = "0.7.2" 15 | lazy_static = { version = "1.4.0", features = ["spin_no_std"] } 16 | riscv = { git = "https://github.com/rcore-os/riscv", features = ["inline-asm"] } 17 | spin = "0.5.2" 18 | 19 | # panic 时直接终止,因为我们没有实现堆栈展开的功能 20 | [profile.dev] 21 | panic = "abort" 22 | [profile.release] 23 | panic = "abort" 24 | -------------------------------------------------------------------------------- /03-rustlilngs-exercises/generics/generics2.rs: -------------------------------------------------------------------------------- 1 | // This powerful wrapper provides the ability to store a positive integer value. 2 | // Rewrite it using generics so that it supports wrapping ANY type. 3 | 4 | struct Wrapper { 5 | value: T 6 | } 7 | 8 | impl Wrapper { 9 | pub fn new(value: T) -> Self { 10 | Wrapper { value } 11 | } 12 | } 13 | 14 | #[cfg(test)] 15 | mod tests { 16 | use super::*; 17 | 18 | #[test] 19 | fn store_u32_in_wrapper() { 20 | assert_eq!(Wrapper::new(42).value, 42); 21 | } 22 | 23 | #[test] 24 | fn store_str_in_wrapper() { 25 | // TODO: Delete this assert and uncomment the one below once you have finished the exercise. 26 | assert_eq!(Wrapper::new("Foo").value, "Foo"); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /04-learn-rust-the-hard-way/ex04.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let cars = 100; 3 | let space_in_a_car = 4.0; 4 | let drivers = 30; 5 | let passengers = 90; 6 | let cars_not_driven = cars - drivers; 7 | let cars_driven = drivers; 8 | let carpool_capacity = (cars_driven as f64) * space_in_a_car; 9 | let average_passengers_per_car = passengers / cars_driven; 10 | 11 | println!("There are {} cars available.", cars); 12 | println!("There are only {} drivers available.", drivers); 13 | println!("There will be {} empty cars today.", cars_not_driven); 14 | println!("We can transport {} people today.", carpool_capacity); 15 | println!("We have {} to carpool today.", passengers); 16 | println!("We need to put about {} in each car.", average_passengers_per_car); 17 | } 18 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter06-conversion/conversion_2.rs: -------------------------------------------------------------------------------- 1 | use std::convert::{TryFrom, TryInto}; 2 | 3 | #[derive(Debug, PartialEq)] 4 | struct EvenNumber(i32); 5 | 6 | impl TryFrom for EvenNumber { 7 | type Error = (); 8 | 9 | fn try_from(value: i32) -> Result { 10 | if value % 2 == 0 { 11 | Ok(EvenNumber(value)) 12 | } else { 13 | Err(()) 14 | } 15 | } 16 | } 17 | 18 | fn main() { 19 | assert_eq!(EvenNumber::try_from(8), Ok(EvenNumber(8))); 20 | assert_eq!(EvenNumber::try_from(5), Err(())); 21 | 22 | let result: Result = 8i32.try_into(); 23 | assert_eq!(result, Ok(EvenNumber(8))); 24 | let result: Result = 5i32.try_into(); 25 | assert_eq!(result, Err(())); 26 | } 27 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3/os/src/memory/mod.rs: -------------------------------------------------------------------------------- 1 | //! 内存管理模块 2 | //! 3 | //! 负责空间分配和虚拟地址映射 4 | 5 | // 因为模块内包含许多基础设施类别,实现了许多以后可能会用到的函数, 6 | // 所以在模块范围内不提示「未使用的函数」等警告 7 | #![allow(dead_code)] 8 | 9 | pub mod address; 10 | pub mod config; 11 | pub mod frame; 12 | pub mod heap; 13 | pub mod mapping; 14 | pub mod range; 15 | 16 | /// 一个缩写,模块中一些函数会使用 17 | pub type MemoryResult = Result; 18 | 19 | pub use { 20 | address::*, 21 | config::*, 22 | frame::FRAME_ALLOCATOR, 23 | mapping::{Flags, MapType, MemorySet, Segment}, 24 | range::Range, 25 | }; 26 | 27 | /// 初始化内存相关的子模块 28 | /// 29 | /// - [`heap::init`] 30 | pub fn init() { 31 | heap::init(); 32 | // 允许内核读写用户态内存 33 | unsafe { riscv::register::sstatus::set_sum() }; 34 | 35 | println!("mod memory initialized"); 36 | } 37 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab4/os/src/memory/mod.rs: -------------------------------------------------------------------------------- 1 | //! 内存管理模块 2 | //! 3 | //! 负责空间分配和虚拟地址映射 4 | 5 | // 因为模块内包含许多基础设施类别,实现了许多以后可能会用到的函数, 6 | // 所以在模块范围内不提示「未使用的函数」等警告 7 | #![allow(dead_code)] 8 | 9 | pub mod address; 10 | pub mod config; 11 | pub mod frame; 12 | pub mod heap; 13 | pub mod mapping; 14 | pub mod range; 15 | 16 | /// 一个缩写,模块中一些函数会使用 17 | pub type MemoryResult = Result; 18 | 19 | pub use { 20 | address::*, 21 | config::*, 22 | frame::FRAME_ALLOCATOR, 23 | mapping::{Flags, MapType, MemorySet, Segment}, 24 | range::Range, 25 | }; 26 | 27 | /// 初始化内存相关的子模块 28 | /// 29 | /// - [`heap::init`] 30 | pub fn init() { 31 | heap::init(); 32 | // 允许内核读写用户态内存 33 | unsafe { riscv::register::sstatus::set_sum() }; 34 | 35 | println!("mod memory initialized"); 36 | } 37 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab5/os/src/memory/mod.rs: -------------------------------------------------------------------------------- 1 | //! 内存管理模块 2 | //! 3 | //! 负责空间分配和虚拟地址映射 4 | 5 | // 因为模块内包含许多基础设施类别,实现了许多以后可能会用到的函数, 6 | // 所以在模块范围内不提示「未使用的函数」等警告 7 | #![allow(dead_code)] 8 | 9 | pub mod address; 10 | pub mod config; 11 | pub mod frame; 12 | pub mod heap; 13 | pub mod mapping; 14 | pub mod range; 15 | 16 | /// 一个缩写,模块中一些函数会使用 17 | pub type MemoryResult = Result; 18 | 19 | pub use { 20 | address::*, 21 | config::*, 22 | frame::FRAME_ALLOCATOR, 23 | mapping::{Flags, MapType, MemorySet, Segment}, 24 | range::Range, 25 | }; 26 | 27 | /// 初始化内存相关的子模块 28 | /// 29 | /// - [`heap::init`] 30 | pub fn init() { 31 | heap::init(); 32 | // 允许内核读写用户态内存 33 | unsafe { riscv::register::sstatus::set_sum() }; 34 | 35 | println!("mod memory initialized"); 36 | } 37 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab6/os/src/memory/mod.rs: -------------------------------------------------------------------------------- 1 | //! 内存管理模块 2 | //! 3 | //! 负责空间分配和虚拟地址映射 4 | 5 | // 因为模块内包含许多基础设施类别,实现了许多以后可能会用到的函数, 6 | // 所以在模块范围内不提示「未使用的函数」等警告 7 | #![allow(dead_code)] 8 | 9 | pub mod address; 10 | pub mod config; 11 | pub mod frame; 12 | pub mod heap; 13 | pub mod mapping; 14 | pub mod range; 15 | 16 | /// 一个缩写,模块中一些函数会使用 17 | pub type MemoryResult = Result; 18 | 19 | pub use { 20 | address::*, 21 | config::*, 22 | frame::FRAME_ALLOCATOR, 23 | mapping::{Flags, MapType, MemorySet, Segment}, 24 | range::Range, 25 | }; 26 | 27 | /// 初始化内存相关的子模块 28 | /// 29 | /// - [`heap::init`] 30 | pub fn init() { 31 | heap::init(); 32 | // 允许内核读写用户态内存 33 | unsafe { riscv::register::sstatus::set_sum() }; 34 | 35 | println!("mod memory initialized"); 36 | } 37 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter09-fn/fn_11.rs: -------------------------------------------------------------------------------- 1 | fn is_odd(n: u32) -> bool { 2 | n % 2 == 1 3 | } 4 | 5 | fn main() { 6 | println!("Find the sum of all the squared odd numbers under 1000"); 7 | let upper = 1000; 8 | 9 | let mut acc = 0; 10 | for n in 0.. { 11 | let n_squared = n * n; 12 | 13 | if n_squared >= upper { 14 | break; 15 | } else if is_odd(n_squared) { 16 | acc += n_squared; 17 | } 18 | } 19 | println!("imperative style: {}", acc); 20 | 21 | let sum_of_squared_odd_numbers: u32 = 22 | (0..).map(|n| n * n) 23 | .take_while(|&n| n < upper) 24 | .filter(|&n| is_odd(n)) 25 | .fold(0, |sum, i| sum + i); 26 | println!("functional style: {}", sum_of_squared_odd_numbers); 27 | } 28 | 29 | -------------------------------------------------------------------------------- /01-rust-by-example/chapter10-mod/mod_2.rs: -------------------------------------------------------------------------------- 1 | mod my { 2 | pub struct OpenBox { 3 | pub contents: T, 4 | } 5 | 6 | pub struct ClosedBox { 7 | contents: T, 8 | } 9 | 10 | impl ClosedBox { 11 | pub fn new(contents: T) -> ClosedBox { 12 | ClosedBox { 13 | contents: contents, 14 | } 15 | } 16 | } 17 | } 18 | 19 | fn main() { 20 | let open_box = my::OpenBox { contents: "public information" }; 21 | 22 | println!("The open box contains: {}", open_box.contents); 23 | 24 | // let closed_box = my::ClosedBox { contents: "classified information" }; 25 | 26 | let _closed_box = my::ClosedBox::new("classified information"); 27 | 28 | // println!("The closed box contains: {}", _closed_box.contents); 29 | } 30 | 31 | -------------------------------------------------------------------------------- /06-rcore-lab-notes/lab3_practice/os/src/memory/mod.rs: -------------------------------------------------------------------------------- 1 | //! 内存管理模块 2 | //! 3 | //! 负责空间分配和虚拟地址映射 4 | 5 | // 因为模块内包含许多基础设施类别,实现了许多以后可能会用到的函数, 6 | // 所以在模块范围内不提示「未使用的函数」等警告 7 | #![allow(dead_code)] 8 | 9 | pub mod address; 10 | pub mod config; 11 | pub mod frame; 12 | pub mod heap; 13 | pub mod mapping; 14 | pub mod range; 15 | 16 | /// 一个缩写,模块中一些函数会使用 17 | pub type MemoryResult = Result; 18 | 19 | pub use { 20 | address::*, 21 | config::*, 22 | frame::FRAME_ALLOCATOR, 23 | mapping::{Flags, MapType, MemorySet, Segment}, 24 | range::Range, 25 | }; 26 | 27 | /// 初始化内存相关的子模块 28 | /// 29 | /// - [`heap::init`] 30 | pub fn init() { 31 | heap::init(); 32 | // 允许内核读写用户态内存 33 | unsafe { riscv::register::sstatus::set_sum() }; 34 | 35 | println!("mod memory initialized"); 36 | } 37 | --------------------------------------------------------------------------------