Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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:
学习节奏建议:

ChaptersTopicSuggested TimeCheckpoint
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. 引言与动机 🟢

2. Getting Started 🟢
2. 快速开始 🟢

3. Built-in Types and Variables 🟢
3. 内置类型与变量 🟢

4. Control Flow 🟢
4. 控制流 🟢

5. Data Structures and Collections 🟢
5. 数据结构与集合 🟢

6. Enums and Pattern Matching 🟡
6. 枚举与模式匹配 🟡

Part II — Core Concepts
第二部分:核心概念

7. Ownership and Borrowing 🟡
7. 所有权与借用 🟡

8. Crates and Modules 🟢
8. crate 与模块 🟢

9. Error Handling 🟡
9. 错误处理 🟡

10. Traits and Generics 🟡
10. Trait 与泛型 🟡

11. From and Into Traits 🟡
11. From 与 Into Trait 🟡

12. Closures and Iterators 🟡
12. 闭包与迭代器 🟡

Part III — Advanced Topics & Migration
第三部分:进阶主题与迁移

13. Concurrency 🔴
13. 并发 🔴

14. Unsafe Rust, FFI, and Testing 🔴
14. Unsafe Rust、FFI 与测试 🔴

15. Migration Patterns 🟡
15. 迁移模式 🟡

16. Best Practices 🟡
16. 最佳实践 🟡


Part IV — Capstone
第四部分:综合项目

17. Capstone Project: CLI Task Manager 🔴
17. 综合项目:命令行任务管理器 🔴