axum for type-safe async HTTP servers

Marcus Chen Jan 2026
1 tab
use axum::{routing::get, Router, Json};
use serde::Serialize;

#[derive(Serialize)]
struct Response {
    message: String,
}

async fn handler() -> Json<Response> {
    Json(Response { message: "Hello, axum!".into() })
}

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(handler));
    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
        .serve(app.into_make_service())
        .await
        .unwrap();
}
1 file · rust Explain with highlit

Axum is a modern web framework built on tokio and hyper. It uses extractors (like Json<T>, Path<T>, State<S>) to parse requests into Rust types, and the compiler ensures your handlers match their routes. Middleware is just functions, making it easy to compose. I like axum because it's minimalist but ergonomic: no macros, just traits and async functions. The Router API is clean, and integration with tower services gives you battle-tested middleware (tracing, timeouts, rate limiting). For APIs, I define request/response types as structs, and axum handles serialization with serde_json. It's my go-to for building production HTTP services in Rust because the type safety catches bugs early and the performance is excellent.