Rust Bootstrap Course for C/C++ Programmers / 面向 C/C++ 程序员的 Rust 入门强化课程
Course Overview / 课程概览
- Course overview / 课程包含以下内容
- The case for Rust (from both C and C++ perspectives) / 为什么选择 Rust(从 C 和 C++ 两个角度出发)
- Local installation / 本地安装
- Types, functions, control flow, pattern matching / 类型、函数、控制流、模式匹配
- Modules, cargo / 模块与 Cargo
- Traits, generics / Trait 与泛型
- Collections, error handling / 集合与错误处理
- Closures, memory management, lifetimes, smart pointers / 闭包、内存管理、生命周期、智能指针
- Concurrency / 并发
- Unsafe Rust, including Foreign Function Interface (FFI) / Unsafe Rust,包括外部函数接口(FFI)
no_stdand embedded Rust essentials for firmware teams / 面向固件团队的no_std与嵌入式 Rust 核心内容- Case studies: real-world C++ to Rust translation patterns / 案例研究:真实世界中的 C++ 到 Rust 迁移模式
- We’ll not cover
asyncRust in this course - see the companion Async Rust Training for a full treatment of futures, executors,Pin, tokio, and production async patterns - 本课程不讲解
asyncRust;如需系统学习 futures、执行器、Pin、tokio 与生产级异步模式,请参考配套的 Async Rust Training
Self-Study Guide / 自学指南
This material works both as an instructor-led course and for self-study. If you’re working through it on your own, here’s how to get the most out of it:
本材料既适合讲师授课,也适合自学。如果你打算独立学习,下面的建议可以帮助你获得更好的学习效果:
Pacing recommendations / 学习节奏建议:
| Chapters / 章节 | Topic / 主题 | Suggested Time / 建议时间 | Checkpoint / 检查点 |
|---|---|---|---|
| 1-4 | Setup, types, control flow / 环境、类型、控制流 | 1 day / 1 天 | You can write a CLI temperature converter / 你可以写出一个命令行温度转换器 |
| 5-7 | Data structures, ownership / 数据结构、所有权 | 1-2 days / 1-2 天 | You can explain why let s2 = s1 invalidates s1 / 你可以解释为什么 let s2 = s1 会使 s1 失效 |
| 8-9 | Modules, error handling / 模块、错误处理 | 1 day / 1 天 | You can create a multi-file project that propagates errors with ? / 你可以创建一个多文件项目并用 ? 传播错误 |
| 10-12 | Traits, generics, closures / Trait、泛型、闭包 | 1-2 days / 1-2 天 | You can write a generic function with trait bounds / 你可以写出带 trait 约束的泛型函数 |
| 13-14 | Concurrency, unsafe/FFI / 并发、unsafe/FFI | 1 day / 1 天 | You can write a thread-safe counter with Arc<Mutex<T>> / 你可以用 Arc<Mutex<T>> 写出线程安全计数器 |
| 15-16 | Deep dives / 深入专题 | At your own pace / 自定节奏 | Reference material - read when relevant / 作为参考材料,在需要时查阅 |
| 17-19 | Best practices & reference / 最佳实践与参考 | At your own pace / 自定节奏 | Consult as you write real code / 在实际写代码时按需查阅 |
How to use the exercises / 如何使用练习:
- Every chapter has hands-on exercises marked with difficulty: 🟢 Starter, 🟡 Intermediate, 🔶 Challenge / 每章都包含带难度标记的动手练习:🟢 入门、🟡 中级、🔶 挑战
- Always try the exercise before expanding the solution. Struggling with the borrow checker is part of learning - the compiler’s error messages are your teacher / 总是先做题,再看答案。 与借用检查器“拉扯”是学习的一部分,编译器的报错就是你的老师
- If you’re stuck for more than 15 minutes, expand the solution, study it, then close it and try again from scratch / 如果卡住超过 15 分钟,就先看答案、理解思路,再关闭答案重新独立完成
- The Rust Playground lets you run code without a local install / Rust Playground 允许你无需本地安装就运行代码
When you hit a wall / 遇到难点时:
- Read the compiler error message carefully - Rust’s errors are exceptionally helpful / 仔细阅读编译器错误信息,Rust 的错误提示通常非常有帮助
- Re-read the relevant section; concepts like ownership (ch7) often click on the second pass / 重读相关章节,像所有权这类概念常常在第二遍时真正理解
- The Rust standard library docs are excellent - search for any type or method / Rust 标准库文档 非常优秀,任何类型或方法都值得查阅
- For async patterns, see the companion Async Rust Training / 如需学习异步模式,请参考配套的 Async Rust Training
Table of Contents / 目录
Part I - Foundations / 第一部分:基础
1. Introduction and Motivation / 1. 引言与动机
- Speaker intro and general approach / 讲师介绍与整体方法
- The case for Rust / 为什么选择 Rust
- How does Rust address these issues? / Rust 如何解决这些问题?
- Other Rust USPs and features / Rust 的其他独特优势与特性
- Quick Reference: Rust vs C/C++ / 速查:Rust 与 C/C++ 对比
- Why C/C++ Developers Need Rust / 为什么 C/C++ 开发者需要 Rust
2. Getting Started / 2. 快速开始
- Enough talk already: Show me some code / 少说多练:先看代码
- Rust Local installation / Rust 本地安装
- Rust packages (crates) / Rust 包(crate)
- Example: cargo and crates / 示例:cargo 与 crate
3. Basic Types and Variables / 3. 基础类型与变量
- Built-in Rust types / Rust 内建类型
- Rust type specification and assignment / Rust 类型标注与赋值
- Rust type specification and inference / Rust 类型标注与推断
- Rust variables and mutability / Rust 变量与可变性
4. Control Flow / 4. 控制流
- Rust if keyword / Rust
if关键字 - Rust loops using while and for / 使用 while 与 for 的 Rust 循环
- Rust loops using loop / 使用 loop 的 Rust 循环
- Rust expression blocks / Rust 表达式块
5. Data Structures and Collections / 5. 数据结构与集合
- Rust array type / Rust 数组类型
- Rust tuples / Rust 元组
- Rust references / Rust 引用
- C++ References vs Rust References - Key Differences / C++ 引用与 Rust 引用的关键区别
- Rust slices / Rust 切片
- Rust constants and statics / Rust 常量与静态项
- Rust strings: String vs &str / Rust 字符串:String 与 &str
- Rust structs / Rust 结构体
- Rust Vec
/ Rust Vec - Rust HashMap / Rust HashMap
- Exercise: Vec and HashMap / 练习:Vec 与 HashMap
6. Pattern Matching and Enums / 6. 模式匹配与枚举
- Rust enum types / Rust 枚举类型
- Rust match statement / Rust
match语句 - Exercise: Implement add and subtract using match and enum / 练习:使用 match 和 enum 实现加减法
7. Ownership and Memory Management / 7. 所有权与内存管理
- Rust memory management / Rust 内存管理
- Rust ownership, borrowing and lifetimes / Rust 所有权、借用与生命周期
- Rust move semantics / Rust 移动语义
- Rust Clone / Rust Clone
- Rust Copy trait / Rust Copy trait
- Rust Drop trait / Rust Drop trait
- Exercise: Move, Copy and Drop / 练习:Move、Copy 与 Drop
- Rust lifetime and borrowing / Rust 生命周期与借用
- Rust lifetime annotations / Rust 生命周期标注
- Exercise: Slice storage with lifetimes / 练习:带生命周期的切片存储
- Lifetime Elision Rules Deep Dive / 生命周期省略规则深入解析
- Rust Box
/ Rust Box - Interior Mutability: Cell
and RefCell / 内部可变性:Cell 与 RefCell - Shared Ownership: Rc
/ 共享所有权:Rc - Exercise: Shared ownership and interior mutability / 练习:共享所有权与内部可变性
8. Modules and Crates / 8. 模块与 Crate
- Rust crates and modules / Rust crate 与模块
- Exercise: Modules and functions / 练习:模块与函数
- Workspaces and crates (packages) / 工作区与 crate(包)
- Exercise: Using workspaces and package dependencies / 练习:使用工作区与包依赖
- Using community crates from crates.io / 使用 crates.io 上的社区 crate
- Crates dependencies and SemVer / Crate 依赖与 SemVer
- Exercise: Using the rand crate / 练习:使用 rand crate
- Cargo.toml and Cargo.lock / Cargo.toml 与 Cargo.lock
- Cargo test feature / Cargo test 功能
- Other Cargo features / 其他 Cargo 功能
- Testing Patterns / 测试模式
9. Error Handling / 9. 错误处理
- Connecting enums to Option and Result / 将枚举与 Option、Result 关联起来
- Rust Option type / Rust Option 类型
- Rust Result type / Rust Result 类型
- Exercise: log() function implementation with Option / 练习:使用 Option 实现
log()函数 - Rust error handling / Rust 错误处理
- Exercise: error handling / 练习:错误处理
- Error Handling Best Practices / 错误处理最佳实践
10. Traits and Generics / 10. Trait 与泛型
- Rust traits / Rust trait
- C++ Operator Overloading to Rust std::ops Traits / C++ 运算符重载到 Rust
std::opstrait - Exercise: Logger trait implementation / 练习:实现 Logger trait
- When to use enum vs dyn Trait / 何时使用 enum,何时使用 dyn Trait
- Exercise: Think Before You Translate / 练习:翻译代码前先思考
- Rust generics / Rust 泛型
- Exercise: Generics / 练习:泛型
- Combining Rust traits and generics / 结合 Rust trait 与泛型
- Rust traits constraints in data types / 数据类型中的 Rust trait 约束
- Exercise: Trait constraints and generics / 练习:trait 约束与泛型
- Rust type state pattern and generics / Rust type-state 模式与泛型
- Rust builder pattern / Rust builder 模式
11. Type System Advanced Features / 11. 类型系统高级特性
- Rust From and Into traits / Rust From 与 Into trait
- Exercise: From and Into / 练习:From 与 Into
- Rust Default trait / Rust Default trait
- Other Rust type conversions / 其他 Rust 类型转换
12. Functional Programming / 12. 函数式编程
- Rust closures / Rust 闭包
- Exercise: Closures and capturing / 练习:闭包与捕获
- Rust iterators / Rust 迭代器
- Exercise: Rust iterators / 练习:Rust 迭代器
- Iterator Power Tools Reference / 迭代器进阶工具速查
13. Concurrency / 13. 并发
- Rust concurrency / Rust 并发
- Why Rust prevents data races: Send and Sync / Rust 为什么能防止数据竞争:Send 与 Sync
- Exercise: Multi-threaded word count / 练习:多线程词频统计
14. Unsafe Rust and FFI / 14. Unsafe Rust 与 FFI
- Unsafe Rust / Unsafe Rust
- Simple FFI example / 简单 FFI 示例
- Complex FFI example / 复杂 FFI 示例
- Ensuring correctness of unsafe code / 确保 unsafe 代码正确性
- Exercise: Writing a safe FFI wrapper / 练习:编写安全的 FFI 包装器
Part II - Deep Dives / 第二部分:深入专题
15. no_std - Rust for Bare Metal / 15. no_std:面向裸机的 Rust
- What is no_std? / 什么是 no_std?
- When to use no_std vs std / 何时使用 no_std,何时使用 std
- Exercise: no_std ring buffer / 练习:no_std 环形缓冲区
- Embedded Deep Dive / 嵌入式深入解析
16. Case Studies: Real-World C++ to Rust Translation / 16. 案例研究:真实世界中的 C++ 到 Rust 迁移
- Case Study 1: Inheritance hierarchy to Enum dispatch / 案例 1:继承层次到枚举分发
- Case Study 2: shared_ptr tree to Arena/index pattern / 案例 2:shared_ptr 树到 Arena/索引模式
- Case Study 3: Framework communication to Lifetime borrowing / 案例 3:框架通信到生命周期借用
- Case Study 4: God object to Composable state / 案例 4:上帝对象到可组合状态
- Case Study 5: Trait objects - when they ARE right / 案例 5:Trait 对象何时才是正确选择
Part III - Best Practices & Reference / 第三部分:最佳实践与参考
17. Best Practices / 17. 最佳实践
- Rust Best Practices Summary / Rust 最佳实践总结
- Avoiding excessive clone() / 避免过度使用 clone()
- Avoiding unchecked indexing / 避免未检查索引
- Collapsing assignment pyramids / 精简赋值金字塔
- Capstone Exercise: Diagnostic Event Pipeline / 综合练习:诊断事件流水线
- Logging and Tracing Ecosystem / 日志与追踪生态