Rust for Python Programmers: Complete Training Guide
Rust 面向 Python 程序员:完整训练指南
A comprehensive guide to learning Rust for developers with Python experience. This guide
covers everything from basic syntax to advanced patterns, focusing on the conceptual shifts
required when moving from a dynamically-typed, garbage-collected language to a statically-typed
systems language with compile-time memory safety.
这是一本面向 Python 开发者的 Rust 全面训练指南,内容从基础语法覆盖到高级模式,重点讲清从动态类型、垃圾回收语言迁移到静态类型、具备编译期内存安全保证的系统语言时,思维方式到底要怎么切换。
How to Use This Book
如何使用本书
Self-study format: Work through Part I (ch 1–6) first — these map closely to Python concepts you already know. Part II (ch 7–12) introduces Rust-specific ideas like ownership and traits. Part III (ch 13–16) covers advanced topics and migration.
自学建议:先读第一部分,也就是第 1–6 章,这一段和 Python 已有经验贴得最近。第二部分,也就是第 7–12 章,会进入 Rust 自己那套核心概念,比如所有权和 trait。第三部分,也就是第 13–16 章,则开始处理进阶主题和迁移实践。
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 in Rust 能够用 Rust 写一个命令行温度转换器。 |
| 5–6 第 5–6 章 | Data structures, enums, pattern matching 数据结构、枚举与模式匹配 | 1–2 days 1–2 天 | You can define an enum with data and match exhaustively on it能够定义携带数据的枚举,并用 match 做穷尽匹配。 |
| 7 第 7 章 | Ownership and borrowing 所有权与借用 | 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, iterators Trait、泛型、闭包与迭代器 | 1–2 days 1–2 天 | You can translate a list comprehension to an iterator chain 能够把列表推导式翻译成迭代器链。 |
| 13 第 13 章 | Concurrency 并发 | 1 day 1 天 | You can write a thread-safe counter with Arc<Mutex<T>>能够用 Arc<Mutex<T>> 写出线程安全计数器。 |
| 14 第 14 章 | Unsafe, PyO3, testing Unsafe、PyO3 与测试 | 1 day 1 天 | You can call a Rust function from Python via PyO3 能够通过 PyO3 从 Python 调用 Rust 函数。 |
| 15–16 第 15–16 章 | Migration, best practices 迁移与最佳实践 | At your own pace 按个人节奏 | Reference material — consult as you write real code 属于参考型内容,写真实项目时可以反复查阅。 |
| 17 第 17 章 | Capstone project 综合项目 | 2–3 days 2–3 天 | Build a complete CLI app tying everything together 完成一个把全部内容串起来的 CLI 应用。 |
How to use the exercises:
练习怎么做:
- Chapters include hands-on exercises in collapsible
<details>blocks with solutions
每章都配有可折叠<details>练习块,并附带答案。 - 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 可以在不用本地安装环境的情况下直接运行代码。
Difficulty indicators:
难度标记:
- 🟢 Beginner — Direct translation from Python concepts
🟢 初级:几乎可以从 Python 经验直接迁移过来。 - 🟡 Intermediate — Requires understanding ownership or traits
🟡 中级:开始要求理解所有权或 trait。 - 🔴 Advanced — Lifetimes, async internals, or unsafe code
🔴 高级:涉及生命周期、async 内部机制或 unsafe 代码。
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 deeper async patterns, see the companion Async Rust Training
如果想深入 async 模式,可以配合阅读姊妹教材 Async Rust Training。
Table of Contents
目录总览
Part I — Foundations
第一部分:基础
1. Introduction and Motivation 🟢
1. 引言与动机 🟢
- The Case for Rust for Python Developers
Rust 为什么值得 Python 开发者学习 - Common Python Pain Points That Rust Addresses
Rust 能解决哪些 Python 常见痛点 - When to Choose Rust Over Python
什么时候该选 Rust,而不是 Python
2. Getting Started 🟢
2. 快速开始 🟢
- Installation and Setup
安装与环境配置 - Your First Rust Program
第一个 Rust 程序 - Cargo vs pip/Poetry
Cargo 与 pip / Poetry 的对比
3. Built-in Types and Variables 🟢
3. 内置类型与变量 🟢
- Variables and Mutability
变量与可变性 - Primitive Types Comparison
基本类型对照 - String Types: String vs &str
字符串类型:String 与 &str
4. Control Flow 🟢
4. 控制流 🟢
- Conditional Statements
条件语句 - Loops and Iteration
循环与迭代 - Expression Blocks
表达式代码块 - Functions and Type Signatures
函数与类型签名
5. Data Structures and Collections 🟢
5. 数据结构与集合 🟢
- Tuples, Arrays, Slices
元组、数组与切片 - Structs vs Classes
Struct 与 Class 的差异 - Vec vs list, HashMap vs dict
Vec 对比 list,HashMap 对比 dict
6. Enums and Pattern Matching 🟡
6. 枚举与模式匹配 🟡
- Algebraic Data Types vs Union Types
代数数据类型与联合类型 - Exhaustive Pattern Matching
穷尽模式匹配 - Option for None Safety
用 Option 处理 None 安全
Part II — Core Concepts
第二部分:核心概念
7. Ownership and Borrowing 🟡
7. 所有权与借用 🟡
- Understanding Ownership
理解所有权 - Move Semantics vs Reference Counting
移动语义与引用计数 - Borrowing and Lifetimes
借用与生命周期 - Smart Pointers
智能指针
8. Crates and Modules 🟢
8. crate 与模块 🟢
- Rust Modules vs Python Packages
Rust 模块与 Python 包 - Crates vs PyPI Packages
crate 与 PyPI 包
9. Error Handling 🟡
9. 错误处理 🟡
- Exceptions vs Result
异常与 Result 的差异 - The ? Operator
?运算符 - Custom Error Types with thiserror
用 thiserror 定义自定义错误类型
10. Traits and Generics 🟡
10. Trait 与泛型 🟡
- Traits vs Duck Typing
Trait 与鸭子类型对比 - Protocols (PEP 544) vs Traits
Protocol 与 Trait 对比 - Generic Constraints
泛型约束
11. From and Into Traits 🟡
11. From 与 Into Trait 🟡
- Type Conversions in Rust
Rust 中的类型转换 - From, Into, TryFrom
From、Into、TryFrom - String Conversion Patterns
字符串转换模式
12. Closures and Iterators 🟡
12. 闭包与迭代器 🟡
- Closures vs Lambdas
闭包与 lambda - Iterators vs Generators
迭代器与生成器 - Macros: Code That Writes Code
宏:生成代码的代码
Part III — Advanced Topics & Migration
第三部分:进阶主题与迁移
13. Concurrency 🔴
13. 并发 🔴
- No GIL: True Parallelism
没有 GIL:真正的并行 - Thread Safety: Type System Guarantees
线程安全:类型系统保证 - async/await Comparison
async/await 对比
14. Unsafe Rust, FFI, and Testing 🔴
14. Unsafe Rust、FFI 与测试 🔴
- When and Why to Use Unsafe
何时以及为何使用 Unsafe - PyO3: Rust Extensions for Python
PyO3:面向 Python 的 Rust 扩展 - Unit Tests vs pytest
单元测试与 pytest 对比
15. Migration Patterns 🟡
15. 迁移模式 🟡
- Common Python Patterns in Rust
Python 常见模式在 Rust 中的写法 - Essential Crates for Python Developers
Python 开发者必备 crate - Incremental Adoption Strategy
渐进式引入策略
16. Best Practices 🟡
16. 最佳实践 🟡
- Idiomatic Rust for Python Developers
Python 开发者该掌握的 Rust 惯用法 - Common Pitfalls and Solutions
常见陷阱与解决方案 - Python→Rust Rosetta Stone
Python → Rust 对照手册 - Learning Path and Resources
学习路径与资源
Part IV — Capstone
第四部分:综合项目
17. Capstone Project: CLI Task Manager 🔴
17. 综合项目:命令行任务管理器 🔴
- The Project:
rustdo
项目介绍:rustdo - Data Model, Storage, Commands, Business Logic
数据模型、存储、命令与业务逻辑 - Tests and Stretch Goals
测试与延伸目标