0Pricing
Learn Rust Coding · Lesson

Axum Routing

Handlers and routes.

Axum Routing is a free Learn Rust Coding lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Learn Rust Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What is Axum?

Axum is an ergonomic, async web framework from the Tokio team. It builds on tower and hyper.

  • Handlers are plain async functions
  • Routing is type-driven and composable
  • Runs on the Tokio runtime

A handler is a function

An Axum handler is an async function that returns something convertible into a response, such as a &str or String.

async fn hello() -> &'static str {
    "Hello, world!"
}

Building a Router

The Router maps paths to handlers. Use route with an HTTP method filter like get.

use axum::{Router, routing::get};

async fn hello() -> &'static str { "Hello" }

fn app() -> Router {
    Router::new().route("/", get(hello))
}

Starting the server

Bind a TCP listener and serve the router. The #[tokio::main] macro sets up the async runtime.

use axum::{Router, routing::get};

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(|| async { "Hi" }));
    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

Multiple routes

Chain route calls to register several endpoints on one router.

use axum::{Router, routing::get};

fn app() -> Router {
    Router::new()
        .route("/", get(|| async { "home" }))
        .route("/about", get(|| async { "about" }))
}

Different HTTP methods

Combine method handlers on one path with get(...).post(...). Each method routes to its own handler.

use axum::{Router, routing::get};

fn app() -> Router {
    Router::new().route(
        "/items",
        get(|| async { "list" }).post(|| async { "create" }),
    )
}

Path parameters

Capture parts of the URL with a {name} segment and read them via the Path extractor.

use axum::extract::Path;

async fn show(Path(id): Path<u32>) -> String {
    format!("item {}", id)
}

Modeling routing in plain Rust

At its core, routing maps a path to a response. This runnable model captures that idea without the framework.

fn route(path: &str) -> &str {
    match path {
        "/" => "home",
        "/about" => "about page",
        _ => "404 not found",
    }
}

fn main() {
    println!("{}", route("/"));
    println!("{}", route("/about"));
    println!("{}", route("/missing"));
}

Nesting routers

Compose larger apps by mounting a sub-router under a prefix with nest.

use axum::{Router, routing::get};

fn api() -> Router {
    Router::new().route("/users", get(|| async { "users" }))
}

fn app() -> Router {
    Router::new().nest("/api", api())
}

Fallback handler

Define what happens for unmatched routes using fallback. Perfect for custom 404 pages.

use axum::{Router, http::StatusCode};

async fn not_found() -> (StatusCode, &'static str) {
    (StatusCode::NOT_FOUND, "nothing here")
}

fn app() -> Router {
    Router::new().fallback(not_found)
}

Returning status codes

A handler can return a tuple of (StatusCode, body) to control the response status precisely.

use axum::http::StatusCode;

async fn create() -> (StatusCode, &'static str) {
    (StatusCode::CREATED, "created")
}

Quick Check

Test your understanding of Axum routing.

Recap

You learned Axum routing fundamentals:

  • Handlers are async functions returning a response
  • Router::new().route(path, get(handler)) registers endpoints
  • Path captures URL segments
  • nest, fallback, and status tuples shape the app

Next: extractors parse incoming requests.

Frequently asked questions

Is the “Axum Routing” lesson free?

Yes — the full text of “Axum Routing” is free to read here on the web, and the Learn Rust Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Learn Rust Coding course, upgrade to CoddyKit PRO.

What will I learn in “Axum Routing”?

Handlers and routes. You practise Learn Rust Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Learn Rust Coding?

No prior experience is required. Learn Rust Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Axum Routing” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Learn Rust Coding lesson?

Yes. Every Learn Rust Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Axum Routing
  2. Extractors
  3. JSON and State
  4. Middleware
← Back to Learn Rust Coding