本仓库包含 Rust 入门系列文章 的所有配套代码示例。
rust-examples/
├── 01-ownership/ # 所有权 (4 个示例)
├── 02-borrow-checker/ # 借用检查器 (4 个示例)
├── 03-lifetime/ # 生命周期 (3 个示例)
├── 04-smart-pointers/ # 智能指针 (4 个示例)
├── 05-trait/ # Trait (3 个示例)
├── 06-generics-errors/ # 泛型与错误 (3 个示例)
├── 07-memory/ # 内存管理 (3 个示例)
├── 08-concurrency/ # 并发编程 (3 个示例)
├── 09-cargo/ # Cargo (1 个示例)
└── 10-ffi-macros/ # FFI 与宏 (2 个示例)
# 运行特定示例
cd 01-ownership
cargo run --example move_example
# 运行所有示例
cargo run --all-examples
# 运行测试
cargo test --allmove_example- 所有权转移copy_example- Copy traitclone_example- Clone traitownership_quiz- 小测验
immutable_borrow- 不可变借用mutable_borrow- 可变借用borrow_conflict- 借用冲突与修复lifetime_basic- 生命周期基础
lifetime_annot- 生命周期标注lifetime_struct- 结构体生命周期lifetime_elision- 省略规则
box_example- Box 示例rc_example- Rc 引用计数arc_example- Arc 多线程refcell_example- RefCell 内部可变性
trait_basic- Trait 基础trait_object- Trait 对象trait_impl- 常用 Trait 实现
generics_example- 泛型示例result_example- Result 处理option_example- Option 处理
stack_heap- 栈与堆drop_example- Drop traitmemory_layout- 内存布局
thread_example- 线程示例channel_example- 通道示例async_example- 异步示例
workspace_demo- 工作空间演示
ffi_example- FFI 示例macro_example- 宏示例
- 所有示例使用 Rust 2021 Edition
- 需要 Rust 1.70+ 版本
08-concurrency需要网络连接(下载 tokio)
# 格式化代码
cargo fmt --all
# 检查代码
cargo clippy --all
# 运行测试
cargo test --allMIT
配套 Rust 入门系列文章: 查看文章