What you will build#
A program that answers GET / with the text hello from axum on 127.0.0.1:3001.
Before you start#
- Rust installed (
rustc --versionprints a version).
Steps#
1. Create the crate and add the dependencies#
cargo new hello-web
cd hello-web[dependencies]
axum = "0.8.9"
tokio = { version = "1.54.1", features = ["full"] }2. Write the server#
#[tokio::main] starts the async runtime. Router maps a path and method to a handler; the closure || async { ... } is a handler that returns a string.
use axum::{routing::get, Router};
#[tokio::main]
async fn main() {
let app = Router::new().route("/", get(|| async { "hello from axum" }));
let listener = tokio::net::TcpListener::bind("127.0.0.1:3001").await.unwrap();
println!("listening on {}", listener.local_addr().unwrap());
axum::serve(listener, app).await.unwrap();
}3. Run it and call it#
cargo runIn a second terminal, curl -i http://127.0.0.1:3001/ printed:
HTTP/1.1 200 OK
content-type: text/plain; charset=utf-8
content-length: 15
date: Sat, 05 Dec 2026 09:02:16 GMT
hello from axumThe first terminal printed listening on 127.0.0.1:3001. Stop the server with Ctrl+C.
Check that it works#
Open http://127.0.0.1:3001/ in a browser: the page shows hello from axum. Any other path answers 404 with an empty body.
Troubleshooting#
| Symptom | Cause | Fix |
|---|---|---|
the program panics at bind(...).unwrap() with an error about the address being in use (the wording differs by system) | another program, or a second copy of this one, holds port 3001 | stop it or bind a different port |
| the browser cannot connect from another machine | 127.0.0.1 accepts only local connections | bind 0.0.0.0:3001 when outside access is intended |
Going further#
- Next: how routing, path parameters and nested routers work (thread "How do I define routes, path parameters and nested routers in axum?").
- Read the port and address from configuration instead of writing them in the code.
Related questions#
Why does axum need tokio?#
axum::serve and the handlers are async, and something has to run them. tokio is that runtime; #[tokio::main] sets it up around main.
Sources#
- axum,
serve: https://docs.rs/axum/0.8.9/axum/fn.serve.html (read 2026-12-05) - The Rust Book, installation and Cargo: https://doc.rust-lang.org/book/ch01-00-getting-started.html