async/await β Asynchronous Programming in Rust
Future trait-based async model with runtime freedom
Syntax is similar to JavaScript's async/await but internals differ. Rust's async fn compiles to a state machine implementing the Future trait. Heap allocations minimized.
async fn fetch_data(url: &str) -> Result<String, Error> {
let response = reqwest::get(url).await?;
let body = response.text().await?;
Ok(body)
}
Rust's standard library includes no async runtime. Users bring tokio (most popular) or async-std. This design lets embedded environments use lightweight runtimes while servers use high-performance ones.
Pin and Self-Referential Futures
When async blocks hold references to local variables across .await points, self-referential structures form. Pin
Key Points
async fn compiles to a state machine implementing Future trait
.await yields control to runtime β other tasks run
Register async tasks to scheduler with tokio::spawn
#[tokio::main] initializes runtime + makes main async
Pros
- ✓ Zero-cost β Futures compile to state machines, minimal heap allocation
- ✓ Swappable runtimes β tokio, async-std, smol, etc.
Cons
- ✗ async traits were unstable until recently (stabilizing via RPITIT)
- ✗ Pin/Unpin concepts are difficult
- ✗ Ecosystem split between tokio vs async-std libraries