Rust Bootstrap Course for C/C++ Programmers
Rust 面向 C/C++ 程序员入门训练营
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 迁移模式
- The case for Rust (from both C and C++ perspectives)
- 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 第 1–4 章 | Setup, types, control flow 环境准备、类型与控制流 | 1 day 1 天 | You can write a CLI temperature converter 能够写出一个命令行温度转换器。 |
| 5–7 第 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 第 8–9 章 | Modules, error handling 模块与错误处理 | 1 day 1 天 | You can create a multi-file project that propagates errors with ?能够写出一个多文件项目,并用 ? 传播错误。 |
| 10–12 第 10–12 章 | Traits, generics, closures Trait、泛型与闭包 | 1–2 days 1–2 天 | You can write a generic function with trait bounds 能够写出带 trait 约束的泛型函数。 |
| 13–14 第 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 第 15–16 章 | Deep dives 专题深入 | At your own pace 按个人节奏推进 | Reference material — read when relevant 这部分更偏参考材料,遇到相关问题时回来看。 |
| 17–19 第 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 模式上,继续看配套的 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- What Rust Eliminates — The Complete List
Rust 到底消灭了什么:完整清单 - The Problems Shared by C and C++
C 和 C++ 共同存在的问题 - C++ Adds More Problems on Top
C++ 在此基础上又额外引入的问题 - How Rust Addresses All of This
Rust 如何系统性解决这些问题
- What Rust Eliminates — The Complete List
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 loops using loop
使用 loop 的循环 - 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 slice - Rust constants and statics
Rust 常量与静态变量 - Rust strings: String vs &str
Rust 字符串:String与&str - Rust structs
Rust 结构体 - Rust Vec<T>
RustVec<T> - Rust HashMap
RustHashMap - Exercise: Vec and HashMap
练习:Vec与HashMap
6. Pattern Matching and Enums
6. 模式匹配与枚举
- Rust enum types
Rust 枚举类型 - Rust match statement
Rustmatch语句 - Exercise: Implement add and subtract using match and enum
练习:用match与枚举实现加减法
7. Ownership and Memory Management
7. 所有权与内存管理
- Rust memory management
Rust 内存管理 - Rust ownership, borrowing and lifetimes
Rust 所有权、借用与生命周期 - Rust move semantics
Rust 移动语义 - Rust Clone
RustClone - Rust Copy trait
RustCopytrait - Rust Drop trait
RustDroptrait - Exercise: Move, Copy and Drop
练习:Move、Copy 与 Drop - Rust lifetime and borrowing
Rust 生命周期与借用 - Rust lifetime annotations
Rust 生命周期标注 - Exercise: Slice storage with lifetimes
练习:带生命周期的 slice 存储 - Lifetime Elision Rules Deep Dive
生命周期省略规则深入解析 - Rust Box<T>
RustBox<T> - Interior Mutability: Cell<T> and RefCell<T>
内部可变性:Cell<T>与RefCell<T> - Shared Ownership: Rc<T>
共享所有权:Rc<T> - 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)
Workspace 与 crate(package) - Exercise: Using workspaces and package dependencies
练习:使用 workspace 与包依赖 - Using community crates from crates.io
使用 crates.io 社区 crate - Crates dependencies and SemVer
crate 依赖与语义化版本 - Exercise: Using the rand crate
练习:使用randcrate - 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
RustOption类型 - Rust Result type
RustResult类型 - 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 → Rust std::ops Traits
C++ 运算符重载与 Ruststd::opstrait - Exercise: Logger trait implementation
练习:实现Loggertrait - When to use enum vs dyn Trait
何时使用枚举,何时使用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 类型状态模式与泛型 - Rust builder pattern
Rust Builder 模式
11. Type System Advanced Features
11. 类型系统高级特性
- Rust From and Into traits
RustFrom与Intotrait - Exercise: From and Into
练习:From与Into - Rust Default trait
RustDefaulttrait - 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 → Enum dispatch
案例 1:继承层级到枚举分发 - Case Study 2: shared_ptr tree → Arena/index pattern
案例 2:shared_ptr树到 arena/index 模式 - Case Study 3: Framework communication → Lifetime borrowing
案例 3:框架通信到生命周期借用 - Case Study 4: God object → Composable state
案例 4:上帝对象到可组合状态 - Case Study 5: Trait objects — when they ARE right
案例 5:什么时候 trait object 反而是正确选择
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
日志与追踪生态
18. C++ → Rust Semantic Deep Dives
18. C++ → Rust 语义深入对照
- Casting, Preprocessor, Modules, volatile, static, constexpr, SFINAE, and more
类型转换、预处理器、模块、volatile、static、constexpr、SFINAE 等主题
19. Rust Macros
19. Rust 宏
- Declarative macros (
macro_rules!)
声明式宏macro_rules! - Common standard library macros
标准库中的常见宏 - Derive macros
派生宏 - Attribute macros
属性宏 - Procedural macros
过程宏 - When to use what: macros vs functions vs generics
宏、函数与泛型分别适合什么场景 - Exercises
练习