示例索引

每个示例都是 `examples/` workspace 中的 Cargo package,可用统一脚本校验。

00_hello_world 20 分钟

第一个程序:Hello World

运行最小 Rust 程序,认识 main 函数和 println! 宏。

cd examples
cargo run -p rt_00_hello_world
01_cargo_project 25 分钟

Cargo 基础

理解 Cargo 项目的目录、Cargo.toml 和常用命令。

cd examples
cargo run -p rt_01_cargo_project
03_scalar_compound_types 30 分钟

标量类型与复合类型

认识整数、浮点、bool、char、tuple 和 array。

cd examples
cargo run -p rt_03_scalar_compound_types
04_functions 25 分钟

函数、参数与返回值

写出带参数、返回值和表达式函数体的 Rust 函数。

cd examples
cargo run -p rt_04_functions
05_control_flow 30 分钟

控制流

使用 if、loop、while 和 for 表达程序分支与重复。

cd examples
cargo run -p rt_05_control_flow
06_ownership 30 分钟

所有权与移动

理解 Rust 的所有权规则、移动语义和 clone 的代价。

cd examples
cargo run -p rt_06_ownership
07_borrowing 30 分钟

借用与引用

用 & 和 &mut 借用数据,掌握借用检查器的规则。

cd examples
cargo run -p rt_07_borrowing
08_slices 25 分钟

切片

用切片借用数组、Vec 和字符串的一段连续数据。

cd examples
cargo run -p rt_08_slices
09_lifetimes 30 分钟

生命周期入门

用生命周期标注表达引用之间的存活关系。

cd examples
cargo run -p rt_09_lifetimes
10_structs 30 分钟

结构体与方法

用 struct 给数据建模,用 impl 定义方法和关联函数。

cd examples
cargo run -p rt_10_structs
11_enums_option 30 分钟

枚举与 Option

用带数据的枚举建模多态状态,用 Option 表达可能为空。

cd examples
cargo run -p rt_11_enums_option
12_pattern_matching 30 分钟

模式匹配

用 match、if let、守卫和解构写出清晰的分支逻辑。

cd examples
cargo run -p rt_12_pattern_matching
13_collections 30 分钟

常用集合

掌握 Vec、HashMap 和 String 的常见用法。

cd examples
cargo run -p rt_13_collections
14_error_handling 30 分钟

Result 与错误处理

用 Result 和 ? 运算符传播可恢复错误。

cd examples
cargo run -p rt_14_error_handling
15_custom_errors 30 分钟

自定义错误类型

用枚举定义错误,实现 Display 和 Error trait。

cd examples
cargo run -p rt_15_custom_errors
16_generics 30 分钟

泛型

用泛型参数写出适用于多种类型的函数和结构体。

cd examples
cargo run -p rt_16_generics
17_traits 30 分钟

trait 与共享行为

用 trait 定义共享接口和默认方法。

cd examples
cargo run -p rt_17_traits
18_trait_objects 25 分钟

trait 对象与 dyn

用 Box<dyn Trait> 在运行时存放不同类型。

cd examples
cargo run -p rt_18_trait_objects
19_closures 30 分钟

闭包

理解闭包如何捕获环境,以及 Fn、FnMut、FnOnce 的区别。

cd examples
cargo run -p rt_19_closures
20_iterators 30 分钟

迭代器与适配器

用 map、filter、collect、fold 写出声明式的数据处理。

cd examples
cargo run -p rt_20_iterators
21_custom_iterator 25 分钟

自定义迭代器

为自己的类型实现 Iterator trait,复用全部适配器。

cd examples
cargo run -p rt_21_custom_iterator
22_box 25 分钟

Box 与递归类型

用 Box<T> 把数据放到堆上,定义递归数据结构。

cd examples
cargo run -p rt_22_box
23_rc 25 分钟

Rc 共享所有权

用 Rc<T> 让多个所有者共享同一份只读数据。

cd examples
cargo run -p rt_23_rc
24_refcell 30 分钟

RefCell 与内部可变性

用 RefCell<T> 在只持有共享引用时修改数据。

cd examples
cargo run -p rt_24_refcell
25_threads 30 分钟

线程

用 thread::spawn 创建线程,用 join 收集结果。

cd examples
cargo run -p rt_25_threads
26_channels 25 分钟

channel 消息传递

用 mpsc channel 在线程间安全传递数据。

cd examples
cargo run -p rt_26_channels
27_shared_state 30 分钟

共享状态:Arc 与 Mutex

用 Arc<Mutex<T>> 在多线程间安全共享可变数据。

cd examples
cargo run -p rt_27_shared_state
28_modules 30 分钟

模块系统

用 mod、pub 和 use 组织代码,控制可见性。

cd examples
cargo run -p rt_28_modules
29_library 25 分钟

库 crate 与 workspace

区分 lib 与 bin,用 workspace 管理多个 crate。

cd examples
cargo run -p rt_29_library
30_testing 30 分钟

测试

用单元测试和集成测试保证代码正确。

cd examples
cargo run -p rt_30_testing
31_doc_tests 25 分钟

文档与文档测试

用 /// 写文档,让示例代码同时成为测试。

cd examples
cargo run -p rt_31_doc_tests
32_async_await 35 分钟

async/await 基础

用 async/await 和 tokio 运行时编写异步代码。

cd examples
cargo run -p rt_32_async_await
33_async_tasks 30 分钟

并发任务

用 join! 和 spawn 让多个异步任务并发执行。

cd examples
cargo run -p rt_33_async_tasks
35_error_libs 30 分钟

anyhow 与 thiserror

用 thiserror 定义库错误,用 anyhow 聚合应用错误。

cd examples
cargo run -p rt_35_error_libs
36_clap 30 分钟

clap:命令行参数解析

用 clap 的派生宏构建带校验的命令行接口。

cd examples
cargo run -p rt_36_clap
48_uuid 15 分钟

用 uuid 生成唯一标识

用 uuid 生成 v4 随机 UUID、解析与格式化,给实体分配唯一 ID。

cd examples
cargo run -p rt_48_uuid
45_chrono 20 分钟

用 chrono 处理日期与时间

用 chrono 解析、格式化日期时间,并做日期加减与差值计算。

cd examples
cargo run -p rt_45_chrono
49_sha2 15 分钟

用 sha2 计算哈希

用 sha2 计算 SHA-256/512 摘要,并用 hex 编码与校验数据完整性。

cd examples
cargo run -p rt_49_sha2
46_regex 20 分钟

用 regex 处理文本

用 regex 做匹配、查找、捕获组与替换,从文本里提取结构化信息。

cd examples
cargo run -p rt_46_regex
50_rayon 20 分钟

用 rayon 做数据并行

把迭代器链换成 rayon 的并行迭代器,几乎零改动地利用多核加速。

cd examples
cargo run -p rt_50_rayon
47_itertools 20 分钟

用 itertools 增强迭代器

用 itertools 的 unique / counts / chunk_by / sorted / join 等扩展标准迭代器。

cd examples
cargo run -p rt_47_itertools
37_axum 35 分钟

Web 服务:axum

用 axum 构建路由和 JSON 接口,并用 oneshot 测试。

cd examples
cargo run -p rt_37_axum
38_sqlite 30 分钟

数据库:SQLite

用 rusqlite 操作内置 SQLite,完成增查改。

cd examples
cargo run -p rt_38_sqlite
39_macros 30 分钟

声明宏 macro_rules!

用 macro_rules! 在编译期生成重复代码。

cd examples
cargo run -p rt_39_macros
40_unsafe 30 分钟

unsafe 基础

理解 unsafe 的能力边界,以及如何封装成安全接口。

cd examples
cargo run -p rt_40_unsafe
41_ffi 30 分钟

FFI:与 C 互操作

用 extern "C" 调用 C 函数,并把 Rust 函数导出给 C。

cd examples
cargo run -p rt_41_ffi
42_todo_cli 45 分钟

真实项目:命令行待办应用

综合结构体、错误处理、serde 与 clap,做一个可持久化的待办 CLI。

cd examples
cargo run -p rt_42_todo_cli