Axum for Rust: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is Axum and why is it needed?

\n

Axum is a modern, ergonomic, and high-performance web framework for the Rust language, developed by the Tokio team. It is built on top of the popular crates tokio, tower, and hyper, providing high performance and asynchronicity "out of the box".

\n

The main goal of Axum is to provide developers with an intuitive API for building web applications and APIs, while retaining all the power and safety of Rust. Unlike more "low-level" solutions, Axum abstracts the complexity of working with the HTTP protocol, allowing you to focus on business logic.

\n

Axum is ideal for:

\n
    \n
  • Creating RESTful APIs and microservices
  • \n
  • Developing web applications with server-side rendering
  • \n
  • Building proxy servers and middleware
  • \n
  • Implementing WebSocket connections
  • \n
  • Any scenarios requiring high performance and reliability
  • \n
\n\n

Installing Axum

\n

Since Rust is a compiled language, installation is done through the Cargo package manager. Add the following dependencies to your Cargo.toml file:

\n
[dependencies]\naxum = "0.7"\ntokio = { version = "1", features = ["full"] }\nserde = { version = "1", features = ["derive"] }\nserde_json = "1"\ntower-http = { version = "0.5", features = ["cors", "trace"] }\ntracing = "0.1"\ntracing-subscriber = "0.3"
\n

After that, run the command cargo build to download and compile the dependencies.

\n\n

Quick Start: First Application with Axum

\n

Create a file src/main.rs and write a simple HTTP server:

\n
use axum::{\n    routing::get,\n    Router,\n};\nuse std::net::SocketAddr;\n\n#[tokio::main]\nasync fn main() {\n    // Create a router\n    let app = Router::new()\n        .route("/", get(root_handler))\n        .route("/hello/:name", get(hello_handler));\n\n    // Start the server\n    let addr = SocketAddr::from(([127, 0, 0, 1], 3000));\n    println!("Server running on {}", addr);\n    \n    axum::Server::bind(&addr)\n        .serve(app.into_make_service())\n        .await\n        .unwrap();\n}\n\nasync fn root_handler() -> &'static str {\n    "Welcome to the main page!"\n}\n\nasync fn hello_handler(axum::extract::Path(name): axum::extract::Path) -> String {\n    format!("Hello, {}!", name)\n}
\n

Start the server with the command cargo run and open your browser at http://localhost:3000/hello/World.

\n\n

Main Methods and Functions of Axum

\n\n

1. Router::new()

\n

Description: Creates a new empty router. This is the main entry point for configuring your application.

\n

Parameters: None.

\n

Example:

\n
use axum::Router;\n\nlet app: Router = Router::new();
\n\n

2. Router::route()

\n

Description: Adds a route to the specified path with the given HTTP method and handler.

\n

Parameters:

\n
    \n
  • path: &str — the route path (can contain dynamic segments, e.g., /users/:id)
  • \n
  • method_router: MethodRouter — the result of calling get(), post(), put(), delete(), etc.
  • \n
\n

Example:

\n
use axum::{routing::get, Router};\n\nasync fn users_list() -> &'static str { "User list" }\nasync fn create_user() -> &'static str { "Creating user" }\n\nlet app = Router::new()\n    .route("/users", get(users_list).post(create_user));
\n\n

3. routing::get(), post(), put(), delete()

\n

Description: Create a handler for the corresponding HTTP method.

\n

Parameters:

\n

Recommendations