Stage 1 · Lesson 6 of 10
async/await, Tokio and shared state
An function in Rust returns a future, and a future does nothing until something awaits it. Tokio is the runtime that runs them. It spreads many small tasks over a few and parks any task that is waiting on the network or the database.
Axum, SQLx and most Rust network are built on Tokio. tokio::join! runs independent calls at the same time, and tokio::spawn starts a background task. Handlers run on many threads at once, so lives in an Arc, with a Mutex if it must change. The compiler checks that sharing with the Send and Sync traits.
Common trapblocking code inside async functions, such as std::thread::sleep, or a large synchronous file read. It stalls every task on that thread, so move it to spawn_blocking.
A question to check yourself, an AI practice session and 2 cheat sheet entries come with the full roadmap. Unlock
Resources for this stage
Start with· follow one of these
- BookFreeThe Rust Programming Language
The Rust Project · Beginner
The official book, free online: read ownership, enums, error handling and traits slowly
- GuideFreeTokio Tutorial
Tokio · Intermediate
The official Tokio tutorial: tasks, shared state, channels and the async model Axum runs on
Practise
Keep handy
Also worth your time
The DNS, TCP, TLS and HTTP steps behind every request your backend will handle