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.
Instalación lista para agent
Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.
npx -y tokrepo@latest install 412ff341-3634-11f1-9bc6-00163e2b0d79 --target codexEjecutar después de confirmar el plan con dry-run.
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.
Preguntas frecuentes
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.
Referencias (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
Relacionados en TokRepo
Discusión
Activos relacionados
Elysia — Ergonomic TypeScript Web Framework for Bun
Elysia is a fast, ergonomic TypeScript web framework optimized for Bun runtime. End-to-end type safety, OpenAPI support, and performance that rivals Go and Rust frameworks.
Tokio — Async Runtime for Reliable Rust Applications
Tokio is a runtime for writing reliable asynchronous applications with Rust. Provides async I/O, networking, scheduling, and timers. The foundation of most async Rust projects including Axum, Hyper, Tonic, and the broader Rust web ecosystem.
UIkit — Lightweight Modular Front-End Framework
A lightweight and modular front-end framework for developing fast and powerful web interfaces. UIkit provides a comprehensive collection of HTML, CSS, and JS components with consistent styling.
Solidus — Modular Open-Source E-Commerce Framework for Ruby on Rails
Solidus is a free, open-source e-commerce framework built on Ruby on Rails. It provides a complete foundation for online stores with product management, checkout, payments, and shipping, while remaining fully customizable through a modular extension system. Solidus is a community-maintained fork of Spree Commerce.