🦀 Rust Intermediate

How does async/await work in Rust?

Answer

Rust's async/await enables asynchronous programming without blocking threads. Mark a function with async fn to make it return a Future<Output = T> instead of T — the function body does not execute until the future is polled. Use .await inside an async function to suspend execution until a future completes, yielding control to the async runtime to run other tasks. Critically, Rust's async functions are zero-cost: they compile to state machines with no heap allocation overhead per await point (unlike goroutines or threads). Rust's standard library provides the building blocks (Future trait, async/await syntax) but no runtime — you must bring your own executor like Tokio or async-std to drive futures to completion.