Async Rust: From Futures to Production
Async Rust:从 Future 到生产环境
Speaker Intro
讲者简介
- Principal Firmware Architect in Microsoft SCHIE (Silicon and Cloud Hardware Infrastructure Engineering) team
微软 SCHIE 团队首席固件架构师,SCHIE 即 Silicon and Cloud Hardware Infrastructure Engineering。 - Industry veteran with expertise in security, systems programming (firmware, operating systems, hypervisors), CPU and platform architecture, and C++ systems
长期从事安全、系统编程、固件、操作系统、虚拟机监控器、CPU 与平台架构,以及 C++ 系统开发。 - Started programming in Rust in 2017 (@AWS EC2), and have been in love with the language ever since
自 2017 年在 AWS EC2 开始使用 Rust,此后一直深度投入这门语言。
A deep-dive guide to asynchronous programming in Rust. Unlike most async tutorials that start with tokio::main and hand-wave the internals, this guide builds understanding from first principles — the Future trait, polling, state machines — then progresses to real-world patterns, runtime selection, and production pitfalls.
这是一本深入讲解 Rust 异步编程的训练指南。和许多从 tokio::main 起步、对底层一笔带过的教程不同,这本书从第一性原理讲起,先拆清 Future trait、轮询机制和状态机,再逐步推进到真实世界中的模式选择、运行时决策与生产环境问题。
Who This Is For
适合哪些读者
- Rust developers who can write synchronous Rust but find async confusing
已经能够编写同步 Rust,但一碰 async 就开始发懵的 Rust 开发者。 - Developers from C#, Go, Python, or JavaScript who know
async/awaitbut not Rust’s model
来自 C#、Go、Python 或 JavaScript 生态,熟悉async/await,但不了解 Rust 异步模型的开发者。 - Anyone who’s been bitten by
Future is not Send,Pin<Box<dyn Future>>, or “why does my program hang?”
凡是被Future is not Send、Pin<Box<dyn Future>>,或者“程序为什么挂住了”这类问题折腾过的人,都适合读这一套。
Prerequisites
前置知识
You should be comfortable with:
开始阅读前,最好已经具备以下基础:
- Ownership, borrowing, and lifetimes
所有权、借用与生命周期。 - Traits and generics (including
impl Trait)
Trait 与泛型,包括impl Trait。 - Using
Result<T, E>and the?operatorResult<T, E>的使用方式,以及?运算符。 - Basic multi-threading (
std::thread::spawn,Arc,Mutex)
基础多线程知识,例如std::thread::spawn、Arc、Mutex。
No prior async Rust experience is needed.
不要求事先有 Rust 异步编程经验。
How to Use This Book
如何使用本书
Read linearly the first time. Parts I–III build on each other. Each chapter has:
第一次阅读建议按顺序来。 第一到第三部分是逐层递进的,每一章都承担了后面章节的铺垫。
| Symbol | Meaning |
|---|---|
| 🟢 🟢 | Beginner — foundational concept 初级:基础概念,偏入门。 |
| 🟡 🟡 | Intermediate — requires earlier chapters 中级:需要前面章节的基础。 |
| 🔴 🔴 | Advanced — deep internals or production patterns 高级:涉及底层机制或生产模式。 |
Each chapter includes:
每一章通常都包含以下组成部分:
- A “What you’ll learn” block at the top
开头的 “What you’ll learn” 学习目标。 - Mermaid diagrams for visual learners
便于理解流程和结构的 Mermaid 图示。 - An inline exercise with a hidden solution
带隐藏答案的 嵌入式练习。 - Key Takeaways summarizing the core ideas
用于收束重点的 Key Takeaways。 - Cross-references to related chapters
指向相关章节的 交叉引用。
Pacing Guide
学习节奏建议
| Chapters | Topic | Suggested Time | Checkpoint |
|---|---|---|---|
| 1–5 1–5 章 | How Async Works Async 如何工作 | 6–8 hours 6–8 小时 | You can explain Future, Poll, Pin, and why Rust has no built-in runtime能够解释 Future、Poll、Pin,以及为什么 Rust 没有内置运行时。 |
| 6–10 6–10 章 | The Ecosystem 生态体系 | 6–8 hours 6–8 小时 | You can build futures by hand, choose a runtime, and use tokio’s API 能够手写 future、选择运行时,并熟练使用 tokio API。 |
| 11–13 11–13 章 | Production Async 生产环境中的 Async | 6–8 hours 6–8 小时 | You can write production-grade async code with streams, proper error handling, and graceful shutdown 能够写出具备 stream、正确错误处理和优雅停机能力的生产级异步代码。 |
| Capstone 综合项目 | Chat Server 聊天服务器 | 4–6 hours 4–6 小时 | You’ve built a real async application integrating all concepts 已经完成一个整合全部概念的真实异步应用。 |
Total estimated time: 22–30 hours
预计总学习时间:22–30 小时。
Working Through Exercises
练习建议
Every content chapter has an inline exercise. The capstone (Ch 16) integrates everything into a single project. For maximum learning:
每个正文章节都带有嵌入式练习,第 16 章的综合项目则会把全部内容整合到一个完整项目中。为了把收益吃满,建议按下面的节奏来:
- Try the exercise before expanding the solution — struggling is where learning happens
先做题,再看答案。 真正的理解通常发生在卡住和挣扎的时候。 - Type the code, don’t copy-paste — muscle memory matters for Rust’s syntax
手敲代码,不要复制粘贴。 Rust 语法特别依赖肌肉记忆。 - Run every example —
cargo new async-exercisesand test as you go
每个示例都跑一遍。 可以单独建一个cargo new async-exercises工程,边学边验证。
Table of Contents
目录总览
Part I: How Async Works
第一部分:Async 如何工作
- 1. Why Async is Different in Rust 🟢 — The fundamental difference: Rust has no built-in runtime
1. 为什么 Rust 中的 Async 与众不同 🟢 —— 核心差异是:Rust 没有内置运行时。 - 2. The Future Trait 🟡 —
poll(),Waker, and the contract that makes it all work
2. Future Trait 🟡 —— 讲清poll()、Waker以及整套机制依赖的契约。 - 3. How Poll Works 🟡 — The polling state machine and a minimal executor
3. Poll 如何工作 🟡 —— 轮询状态机和一个最小执行器。 - 4. Pin and Unpin 🔴 — Why self-referential structs need pinning
4. Pin 与 Unpin 🔴 —— 为什么自引用结构体需要 pinning。 - 5. The State Machine Reveal 🟢 — What the compiler actually generates from
async fn
5. 状态机真相 🟢 —— 编译器到底从async fn生成了什么。
Part II: The Ecosystem
第二部分:生态体系
- 6. Building Futures by Hand 🟡 — TimerFuture, Join, Select from scratch
6. 手写 Future 🟡 —— 从零实现 TimerFuture、Join、Select。 - 7. Executors and Runtimes 🟡 — tokio, smol, async-std, embassy — how to choose
7. 执行器与运行时 🟡 —— tokio、smol、async-std、embassy 该怎么选。 - 8. Tokio Deep Dive 🟡 — Runtime flavors, spawn, channels, sync primitives
8. Tokio 深入解析 🟡 —— 运行时类型、spawn、channel 与同步原语。 - 9. When Tokio Isn’t the Right Fit 🟡 — LocalSet, FuturesUnordered, runtime-agnostic design
9. Tokio 不合适的场景 🟡 —— LocalSet、FuturesUnordered 与运行时无关设计。 - 10. Async Traits 🟡 — RPITIT, dyn dispatch, trait_variant, async closures
10. Async Trait 🟡 —— RPITIT、dyn 分发、trait_variant 与 async 闭包。
Part III: Production Async
第三部分:生产环境中的 Async
- 11. Streams and AsyncIterator 🟡 — Async iteration, AsyncRead/Write, stream combinators
11. Stream 与 AsyncIterator 🟡 —— 异步迭代、AsyncRead/Write 与 stream 组合器。 - 12. Common Pitfalls 🔴 — 9 production bugs and how to avoid them
12. 常见陷阱 🔴 —— 9 类生产事故及其规避方法。 - 13. Production Patterns 🔴 — Graceful shutdown, backpressure, Tower middleware
13. 生产模式 🔴 —— 优雅停机、背压与 Tower 中间件。
Appendices
附录
- Summary and Reference Card — Quick-lookup tables and decision trees
总结与参考卡片 —— 便于快速查阅的表格和决策树。 - Capstone Project: Async Chat Server — Build a complete async application
综合项目:Async 聊天服务器 —— 构建一个完整的异步应用。