Scripts2026年4月12日·1 分钟阅读

Axum — Ergonomic Modular Web Framework for Rust

Axum is a web application framework built on Tokio, Tower, and Hyper. Focuses on ergonomics and modularity with a macro-free routing API, seamless Tower middleware integration, and type-safe extractors. The official Tokio team web framework.

SC
Script Depot · Community
快速使用

先拿来用,再决定要不要深挖

这里应该同时让用户和 Agent 知道第一步该复制什么、安装什么、落到哪里。

# Cargo.toml
[dependencies]
axum = "0.8"
tokio = { version = "1", features = ["full"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
use axum::{routing::{get, post}, Json, Router};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct Asset {
    repo: String,
    stars: u32,
}

async fn list_assets() -> Json<Vec<Asset>> {
    Json(vec![
        Asset { repo: "react".into(), stars: 230000 },
    ])
}

async fn create_asset(Json(payload): Json<Asset>) -> Json<Asset> {
    Json(payload)
}

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/api/assets", get(list_assets).post(create_asset));

    let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}
介绍

Axum is a web application framework built by the Tokio team. It focuses on ergonomics and modularity with no macros — just functions and extractors. Built on top of Tokio, Tower, and Hyper, Axum integrates seamlessly with the entire Tower middleware ecosystem.

What Axum Does

  • Macro-free routing — plain function handlers
  • Extractors — type-safe request data extraction (Json, Path, Query, State)
  • Tower middleware — use any Tower middleware (timeout, rate-limit, tracing)
  • WebSocket — built-in upgrade support
  • State sharing — via Extension or State extractor
  • Nested routers — composable sub-routers
  • Error handling — typed error responses

Comparison

Framework Macro Tower Tokio-native
Axum None Full Yes
Rocket Heavy No Own runtime
Actix Web Moderate No Own runtime

常见问题 FAQ

Q: 为什么选 Axum? A: Tokio 官方出品,和整个 Tokio 生态(Hyper、Tower、Tonic)无缝集成。如果你已经在用 Tokio,Axum 是最自然的选择。

来源与致谢 Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产