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 / Microsoft 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.

不需要事先具备 async Rust 经验。

How to Use This Book / 如何使用本书

Read linearly the first time. Parts I-II 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 / 顶部的 “你将学到什么” 区块
  • Mermaid diagrams for visual learners / 适合视觉学习者的 Mermaid 图示
  • An inline exercise with a hidden solution / 带隐藏答案的 内联练习
  • Key Takeaways summarizing the core ideas / 总结核心概念的 关键要点
  • Cross-references to related chapters / 指向相关章节的 交叉引用

Pacing Guide / 学习节奏建议

Chapters / 章节Topic / 主题Suggested Time / 建议时间Checkpoint / 检查点
1-5How 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-10The Ecosystem / 生态系统6-8 hours / 6-8 小时You can build futures by hand, choose a runtime, and use tokio’s API / 你可以手写 future、选择运行时并使用 tokio API
11-13Production Async / 生产级 Async6-8 hours / 6-8 小时You can write production-grade async code with streams, proper error handling, and graceful shutdown / 你可以编写包含流、正确错误处理和优雅关闭能力的生产级异步代码
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 example - cargo 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 / 附录