šŸ¦€/⚔/Summary and Reference Card

Summary and Reference Card

Quick Reference Card

Async Mental Model

ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│  async fn → State Machine (enum) → impl Future     │
│  .await   → poll() the inner future                 │
│  executor → loop { poll(); sleep_until_woken(); }   │
│  waker    → "hey executor, poll me again"           │
│  Pin      → "promise I won't move in memory"        │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

Common Patterns Cheat Sheet

GoalUse
Run two futures concurrentlytokio::join!(a, b)
Race two futurestokio::select! { ... }
Spawn a background tasktokio::spawn(async { ... })
Run blocking code in async`tokio::task::spawn_blocking(\
Limit concurrencySemaphore::new(N)
Collect many task resultsJoinSet
Share state across tasksArc<Mutex<T>> or channels
Graceful shutdownwatch::channel + select!
Process a stream N-at-a-time.buffer_unordered(N)
Timeout a futuretokio::time::timeout(dur, fut)
Retry with backoffCustom combinator (see Ch. 13)

Pinning Quick Reference

SituationUse
Pin a future on the heapBox::pin(fut)
Pin a future on the stacktokio::pin!(fut)
Pin an Unpin typePin::new(&mut val) — safe, free
Return a pinned trait object-> Pin<Box<dyn Future<Output = T> + Send>>

Channel Selection Guide

ChannelProducersConsumersValuesUse When
mpscN1StreamWork queues, event buses
oneshot11SingleRequest/response, completion notification
broadcastNNAll recv allFan-out notifications, shutdown signals
watch1NLatest onlyConfig updates, health status

Mutex Selection Guide

MutexUse When
std::sync::MutexLock is held briefly, never across .await
tokio::sync::MutexLock must be held across .await
parking_lot::MutexHigh contention, no .await, need performance
tokio::sync::RwLockMany readers, few writers, locks cross .await

Decision Quick Reference

Need concurrency?
ā”œā”€ā”€ I/O-bound → async/await
ā”œā”€ā”€ CPU-bound → rayon / std::thread
└── Mixed → spawn_blocking for CPU parts

Choosing runtime?
ā”œā”€ā”€ Server app → tokio
ā”œā”€ā”€ Library → runtime-agnostic (futures crate)
ā”œā”€ā”€ Embedded → embassy
└── Minimal → smol

Need concurrent futures?
ā”œā”€ā”€ Can be 'static + Send → tokio::spawn
ā”œā”€ā”€ Can be 'static + !Send → LocalSet
ā”œā”€ā”€ Can't be 'static → FuturesUnordered
└── Need to track/abort → JoinSet

Common Error Messages and Fixes

ErrorCauseFix
future is not SendHolding !Send type across .awaitScope the value so it's dropped before .await, or use current_thread runtime
borrowed value does not live long enough in spawntokio::spawn requires 'staticUse Arc, clone(), or FuturesUnordered
the trait Future is not implemented for ()Missing .awaitAdd .await to the async call
cannot borrow as mutable in pollSelf-referential borrowUse Pin<&mut Self> correctly (see Ch. 4)
Program hangs silentlyForgot to call waker.wake()Ensure every Pending path registers and triggers the waker

Further Reading

ResourceWhy
Tokio TutorialOfficial hands-on guide — excellent for first projects
Async Book (official)Covers Future, Pin, Stream at the language level
Jon Gjengset — Crust of Rust: async/await2-hour deep dive into internals with live coding
Alice Ryhl — Actors with TokioProduction architecture pattern for stateful services
Without Boats — Pin, Unpin, and why Rust needs themThe original motivation from the language designer
Tokio mini-RedisComplete async Rust project — study-quality production code
Tower documentationMiddleware/service architecture used by axum, tonic, hyper

End of Async Rust Training Guide