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

Rocket — A Web Framework for Rust with Focus on Usability

Rocket is a web framework for Rust that makes it simple to write fast, type-safe, secure web applications. Focuses on usability with derive macros, type-safe routing, request guards, and managed state. Intuitive yet powerful.

AI
AI Open Source · Community
快速使用

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

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

# Cargo.toml
[dependencies]
rocket = "0.5"
serde = { version = "1", features = ["derive"] }
rocket_dyn_templates = { version = "0.2", features = ["tera"] }
#[macro_use] extern crate rocket;
use rocket::serde::{json::Json, Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
struct Asset {
    repo: String,
    stars: u32,
}

#[get("/api/assets")]
fn list() -> Json<Vec<Asset>> {
    Json(vec![
        Asset { repo: "react".into(), stars: 230000 },
        Asset { repo: "vue".into(), stars: 210000 },
    ])
}

#[post("/api/assets", format = "json", data = "<asset>")]
fn create(asset: Json<Asset>) -> Json<Asset> {
    Json(asset.into_inner())
}

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/", routes![list, create])
}
介绍

Rocket is a web framework for Rust that makes it simple to write fast, type-safe, secure web applications with incredible usability. Uses Rust macros and the type system to ensure correctness at compile time. Created by Sergio Benitez.

What Rocket Does

  • Type-safe routing — routes validated at compile time
  • Request guards — type-based access to request data
  • Data guards — typed request body parsing
  • Fairings — middleware-like lifecycle callbacks
  • Managed state — thread-safe shared state
  • Templates — Tera, Handlebars via rocket_dyn_templates
  • Async — fully async since Rocket 0.5
  • Forms — multipart, validated form handling
  • Testing — built-in local Client for tests

Comparison

Rust Framework Style Async Macro-heavy
Rocket Rails-like Yes (0.5+) Yes
Axum Tower-based Yes No
Actix Web Actor Yes Moderate
Warp Filter combinators Yes No

常见问题 FAQ

Q: Rocket vs Axum? A: Rocket 更 opinionated、开发速度快(Rails-like DX);Axum 更模块化、和 Tokio/Tower 生态无缝集成。

来源与致谢 Sources

讨论

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

相关资产