Web Framework with Axum

Axum is a modern, ergonomic web framework built on Tokio and tower — it turns ordinary async functions into HTTP handlers with type-safe request extraction and zero boilerplate.

Learn Web Framework with Axum in our free Rust course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Rust course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll build a Router, write handlers, pull data from requests with extractors, return typed responses, and share state across your whole application.

What You'll Learn in This Lesson

1️⃣ A Router and Your First Handler

An Axum app is a Router that maps a path + method to a handler. A handler is just an async fn ; if its return type implements IntoResponse (a String does), it becomes the HTTP response.

axum::serve binds a Tokio TcpListener and drives the router. No threads to manage — Tokio schedules every connection as a task.

2️⃣ Extractors: Path, Query, and Json

Extractors pull typed data out of a request. Path reads dynamic segments like :id , Query parses the query string, and Json deserializes the body. A body-consuming extractor like Json must be the last argument.

Returning Json<User> serializes the struct and sets the JSON content type automatically. Extraction failures (bad JSON, wrong types) become error responses without any extra code.

3️⃣ Shared State and Middleware

Real apps need shared resources — a database pool, config, a counter. Attach them with .with_state(...) and read them through the State extractor. Cross-cutting concerns like logging or timeouts are added as tower .layer(...) middleware.

The state type must be Clone ; an Arc shares one underlying value across every request handled by every worker thread.

Build a route /hello/:name that greets the caller using the Path extractor.

📋 Quick Reference — Axum

Practice quiz

What does Axum's Router do?

  • Maps URL paths and methods to handler functions
  • Serializes JSON
  • Manages async runtimes
  • Connects to a database

Answer: Maps URL paths and methods to handler functions. A Router maps each route (path plus HTTP method) to the handler that serves it.

Which extractor reads a dynamic path segment like /users/:id?

  • Query
  • State
  • Path
  • Json

Answer: Path. The Path extractor pulls typed values out of dynamic path segments.

Which extractor parses the URL query string (?page=2)?

  • Header
  • Query
  • Path
  • Form

Answer: Query. The Query extractor deserializes the query string into a struct.

How does a handler receive a parsed JSON request body?

  • By calling parse()
  • Axum cannot parse JSON
  • By reading State
  • By taking a Json<T> extractor argument

Answer: By taking a Json<T> extractor argument. A Json<T> argument deserializes the request body into T using serde.

What must a handler's return type implement to be a response?

  • IntoResponse
  • FromRow
  • Future
  • Serialize

Answer: IntoResponse. Any handler return type that implements IntoResponse can be turned into an HTTP response.

How do you share application state (like a DB pool) with handlers?

  • Environment variables
  • The State extractor with .with_state(...)
  • Global static mut
  • The Json extractor

Answer: The State extractor with .with_state(...). Attach state with .with_state(...) and read it via the State extractor.

Which method registers a GET handler in Axum?

  • route_get(handler)
  • handle(handler)
  • GET(handler)
  • get(handler)

Answer: get(handler). You write .route(path, get(handler)) using the get() method router.

Returning a tuple like (StatusCode, Json(value)) works because...

  • It is special-cased syntax
  • Tuples are futures
  • Axum implements IntoResponse for such tuples
  • It is converted to a String

Answer: Axum implements IntoResponse for such tuples. Axum implements IntoResponse for tuples of a status code and a body.

Axum is built on top of which lower-level service abstraction?

  • serde
  • tower (Service / Layer)
  • rayon
  • hyper-tls

Answer: tower (Service / Layer). Axum builds on tower, so middleware is added as tower Layers.

How is middleware typically added to an Axum Router?

  • With .layer(...)
  • With .middleware(...)
  • With .use(...)
  • It cannot have middleware

Answer: With .layer(...). Middleware is applied with .layer(...), wrapping the router in a tower Layer.