Async/Await & Tokio in Rust
From the Rust cheat sheet ยท Async Programming ยท verified Jul 2026
Async/Await & Tokio
Write async functions and use the tokio runtime.
rust
// Add to Cargo.toml:
// [dependencies]
// tokio = { version = "1", features = ["full"] }
#[tokio::main]
async fn main() {
let result = fetch_data().await;
println!("{result}");
}
async fn fetch_data() -> String {
// .await pauses until the future completes
"data".to_string()
}๐ก async fn returns a Future โ nothing happens until you .await it
โก tokio::join! runs futures concurrently; sequential .await runs them one after another
๐ Use tokio::sync::Mutex (not std::sync::Mutex) in async code โ it doesn't block the runtime
๐ข tokio::spawn creates lightweight tasks โ much cheaper than OS threads
asyncawaittokiofutures