Build fluency with Axum vocabulary — routing, type-safe extractors, middleware composition, and response traits.
0 / 5 completed
1 / 5
During standup, a Rust backend dev says axum's Router is confusing. How does it actually work?
Router::new() is a builder — you chain .route("/path", get(handler)) calls and use .nest("/prefix", sub_router) for sub-routers. Handlers are plain async fns. Routes are evaluated in insertion order, and axum handles connection polling internally via Tower/Hyper.
2 / 5
In a PR review, a colleague questions your use of the State extractor in an axum handler. What does it do?
State(db): State<Arc<DbPool>> extracts shared state that was registered with .with_state(state) on the Router. Because it's wrapped in Arc, it's cheap to clone per request. This avoids global variables and makes dependencies explicit in function signatures.
3 / 5
During a code review, someone asks why you implemented IntoResponse for a custom type. What's the benefit?
The IntoResponse trait lets axum call .into_response() on whatever your handler returns. Implementing it for custom types (e.g. an AppError enum) means you can write -> Result<Json<T>, AppError> and axum knows how to turn AppError into an HTTP response with the right status code and body.
4 / 5
An incident reveals a bug caused by incorrect middleware layer order in axum. How does .layer() ordering work?
In axum, .layer() wraps the service like an onion. The last.layer() call becomes the outermost layer — it sees the request first on the way in and the response last on the way out. This means CORS should typically be the last layer added so it runs first on incoming requests.
5 / 5
In a design review, the team debates Extension type vs State for passing data in axum. What's the key difference?
State is set once at startup with .with_state() and is the same for all requests. Extension is inserted per-request by middleware (e.g. an auth middleware inserts the authenticated user), making it ideal for request-scoped data. Using the wrong one leads to either missing context or incorrect sharing across requests.