ScriptsApr 12, 2026·2 min read

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.

TL;DR
Axum provides macro-free routing, type-safe extractors, and seamless Tower middleware for building Rust web applications.
§01

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.

§02

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.

§03

How to use

  1. Add axum and tokio to your Cargo.toml.
  2. Define handler functions with typed extractors.
  3. 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();
}
§04

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));
§05

Related on TokRepo

§06

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

How does Axum compare to Actix-web?+

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.

Does Axum support WebSockets?+

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.

Can I use Axum with SQLx or Diesel?+

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.

Is Axum production-ready?+

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.

Does Axum support middleware?+

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

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets