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

Summary and Reference Card

快速参考卡

Async 思维模型

┌─────────────────────────────────────────────────────┐
│  async fn → 状态机(enum) → impl Future            │
│  .await   → poll() 内部 future                       │
│  executor → loop { poll(); sleep_until_woken(); }   │
│  waker    → "嘿 executor,再 poll 我一次"             │
│  Pin      → "承诺我不在内存中移动"                   │
└─────────────────────────────────────────────────────┘

常见模式速查表

目标使用
并发运行两个 futurestokio::join!(a, b)
竞赛两个 futurestokio::select! { ... }
派生后台任务tokio::spawn(async { ... })
在 async 中运行阻塞代码tokio::task::spawn_blocking(|| { ... })
限制并发度Semaphore::new(N)
收集许多任务结果JoinSet
跨任务共享状态Arc<Mutex<T>> 或 channel
优雅关闭watch::channel + select!
一次处理 N 个 stream.buffer_unordered(N)
Timeout 一个 futuretokio::time::timeout(dur, fut)
带退避的重试自定义组合器(见第 13 章)

Pinning 快速参考

情况使用
在堆上 pin futureBox::pin(fut)
在栈上 pin futuretokio::pin!(fut)
Pin 一个 Unpin 类型Pin::new(&mut val) —— 安全,免费
返回 pinned trait object-> Pin<Box<dyn Future<Output = T> + Send>>

Channel 选择指南

Channel生产者消费者值何时使用
mpscN1Stream工作队列、事件总线
oneshot11单个请求/响应、完成通知
broadcastNN所有接收者接收所有Fan-out 通知、关闭信号
watch1N仅最新配置更新、健康状态

Mutex 选择指南

Mutex何时使用
std::sync::Mutex锁持有时间短,从不在 .await 跨 hold
tokio::sync::Mutex锁必须跨 .await 持有
parking_lot::Mutex高竞争,无 .await,需要性能
tokio::sync::RwLock多读少写,锁跨 .await

决策快速参考

需要并发?
├── I/O 绑定 → async/await
├── CPU 绑定 → rayon / std::thread
└── 混合 → CPU 部分用 spawn_blocking

选择 runtime?
├── 服务器应用 → tokio
├── 库 → runtime 无关(futures crate)
├── 嵌入式 → embassy
└── 最小化 → smol

需要并发 futures?
├── 可以是 'static + Send → tokio::spawn
├── 可以是 'static + !Send → LocalSet
├── 不能是 'static → FuturesUnordered
└── 需要跟踪/中止 → JoinSet

常见错误信息和修复

错误原因修复
future is not Send跨 .await 持有 !Send 类型缩小作用域让它在 .await 前 drop,或使用 current_thread runtime
borrowed value does not live long enough 在 spawn 中tokio::spawn 需要 'static使用 Arc、clone(),或 FuturesUnordered
the trait Future is not implemented for ()缺少 .await给 async 调用添加 .await
cannot borrow as mutable 在 poll 中自引用借用正确使用 Pin<&mut Self>(见第 4 章)
程序静默挂起忘记调用 waker.wake()确保每个 Pending 路径注册并触发 waker

进一步阅读

资源为什么
Tokio Tutorial官方实践指南——非常适合入门项目
Async Book (official)在语言层面涵盖 Future、Pin、Stream
Jon Gjengset — Crust of Rust: async/await2 小时内部深入探讨,带现场编码
Alice Ryhl — Actors with Tokio用于状态化服务的生产架构模式
Without Boats — Pin, Unpin, and why Rust needs them来自语言设计者的原始动机
Tokio mini-redis完整的 async Rust 项目——学习级生产代码
Tower documentation被 axum、tonic、hyper 使用的中间件/service 架构

Async Rust 训练指南结束