Axum — Ergonomic Modular Web Framework for Rust
Axum is a web application framework built on Tokio, Tower, and Hyper. Focuses on ergonomics and modularity with a macro-free routing API, seamless Tower middleware integration, and type-safe extractors. The official Tokio team web framework.
What it is
Axum is a web application framework for Rust, built on top of Tokio (async runtime), Tower (middleware), and Hyper (HTTP). It differentiates itself from other Rust web frameworks through a macro-free routing API and type-safe extractors that leverage Rust's type system to parse request data at compile time.
The framework targets Rust developers building HTTP APIs, web services, and backend applications. Its tight integration with the Tower ecosystem means any Tower middleware or service works with Axum out of the box.
How it saves time or tokens
Axum eliminates boilerplate through its extractor pattern. Instead of manually parsing request bodies, query parameters, and headers, you declare them as function parameters and Axum handles deserialization. Compile-time type checking catches request parsing errors before runtime. The Tower middleware reuse means you do not write auth, logging, or rate-limiting from scratch.
How to use
- Add axum and tokio to your Cargo.toml.
- Define handler functions with typed extractors.
- Build a router and start the server.
use axum::{routing::get, Router, extract::Query};
use serde::Deserialize;
#[derive(Deserialize)]
struct Params {
name: String,
}
async fn greet(Query(params): Query<Params>) -> String {
format!("Hello, {}!", params.name)
}
#[tokio::main]
async fn main() {
let app = Router::new().route("/greet", get(greet));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000")
.await.unwrap();
axum::serve(listener, app).await.unwrap();
}
Example
// JSON request and response with validation
use axum::{routing::post, Json};
use serde::{Deserialize, Serialize};
#[derive(Deserialize)]
struct CreateUser { username: String, email: String }
#[derive(Serialize)]
struct User { id: u64, username: String }
async fn create_user(Json(payload): Json<CreateUser>) -> Json<User> {
Json(User { id: 1, username: payload.username })
}
// Router
let app = Router::new().route("/users", post(create_user));
Related on TokRepo
- AI coding tools — Tools that help write and review Rust code with AI
- DevOps tools — Deploy and monitor Axum services in production
Common pitfalls
- Extractors run in the order of function parameters; putting a body extractor (Json) before a non-consuming extractor (Query) works, but using two body extractors in one handler causes a compile error.
- Forgetting to add
.layer()for shared state means handlers cannot access your database pool or config. - Axum requires exact path matching by default; trailing slashes cause 404s unless you add a fallback or normalize middleware.
Frequently Asked Questions
Axum uses the Tower/Hyper ecosystem while Actix-web uses its own runtime and middleware system. Axum's approach makes it composable with any Tower middleware. Actix-web has a larger middleware library built specifically for it. Performance is comparable in most benchmarks.
Yes. Axum provides first-class WebSocket support through the axum::extract::ws module. You upgrade an HTTP connection to a WebSocket in a handler function and process messages asynchronously using Tokio streams.
Yes. You pass a database pool as shared state using Router::with_state() and extract it in handlers. Both SQLx (async) and Diesel (sync via spawn_blocking) work. SQLx is more natural since it is async-native like Axum.
Yes. Axum is maintained by the Tokio project team and used in production by companies running Rust backends. It follows semantic versioning and has reached stable releases. The Tokio, Tower, and Hyper foundations are battle-tested in high-traffic systems.
Yes. Axum uses Tower middleware directly. You can add logging, authentication, rate limiting, CORS, and compression as layers on your router. Any Tower-compatible middleware works without adapter code.
Citations (3)
- Axum GitHub— Axum is built on Tokio, Tower, and Hyper with macro-free routing
- Axum Docs (docs.rs)— Axum extractor pattern and routing documentation
- Tower GitHub— Tower middleware ecosystem for Rust async services
Related on TokRepo
Discussion
Related Assets
WCDB — WeChat Cross-Platform Database Framework
A high-performance, cross-platform database framework developed by WeChat, built on SQLite with ORM, encryption, repair, and migration capabilities.
sql.js — Run SQLite in the Browser with WebAssembly
A JavaScript library that compiles SQLite to WebAssembly, letting you run a full SQL database entirely in the browser or Node.js.
Realm — High-Performance Mobile Database
A fast, object-oriented mobile database designed as a modern replacement for SQLite and Core Data on iOS and Android.