Axum is a fast, ergonomic web framework for Rust built on Tokio (async runtime) and Tower (middleware). In this post we’ll build a minimal “Hello, World!” server you can run in a few minutes.
Setup
Create a new project and add the dependencies:
cargo new hello-axum
cd hello-axumIn Cargo.toml:
[dependencies]
axum = "0.7"
tokio = { version = "1", features = ["full"] }The server
In src/main.rs:
use axum::{routing::get, Router};
#[tokio::main]
async fn main() {
let app = Router::new().route("/", get(handler));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
async fn handler() -> &'static str {
"Hello, World!"
}Run it
cargo runOpen http://localhost:3000 and you should see Hello, World!.
What’s going on
- Router – Defines routes; here we attach a single
GET /tohandler. - handler – An async function that returns a string; Axum sends it as
text/plainwith status 200. - tokio::main – Runs the async runtime so
maincanawait.
From here you can add more routes, JSON with axum::Json, and middleware from the Tower ecosystem.