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

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/await but 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 SendPin<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 ? operator
    Result<T, E> 的使用方式,以及 ? 运算符。
  • Basic multi-threading (std::thread::spawn, Arc, Mutex)
    基础多线程知识,例如 std::thread::spawnArcMutex

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:
第一次阅读建议按顺序来。 第一到第三部分是逐层递进的,每一章都承担了后面章节的铺垫。

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

ChaptersTopicSuggested TimeCheckpoint
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
能够解释 FuturePollPin,以及为什么 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 章的综合项目则会把全部内容整合到一个完整项目中。为了把收益吃满,建议按下面的节奏来:

  1. Try the exercise before expanding the solution — struggling is where learning happens
    先做题,再看答案。 真正的理解通常发生在卡住和挣扎的时候。
  2. Type the code, don’t copy-paste — muscle memory matters for Rust’s syntax
    手敲代码,不要复制粘贴。 Rust 语法特别依赖肌肉记忆。
  3. Run every examplecargo new async-exercises and test as you go
    每个示例都跑一遍。 可以单独建一个 cargo new async-exercises 工程,边学边验证。

Table of Contents
目录总览

Part I: How Async Works
第一部分:Async 如何工作

Part II: The Ecosystem
第二部分:生态体系

Part III: Production Async
第三部分:生产环境中的 Async

Appendices
附录