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.
Agent 可直接安装
这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。
npx -y tokrepo@latest install 412ff08d-3634-11f1-9bc6-00163e2b0d79 --target codex先 dry-run 确认安装计划,再运行此命令。
What it is
Rocket is a web framework for Rust that prioritizes usability without sacrificing speed or safety. It uses derive macros and type-safe routing to catch errors at compile time rather than runtime.
Rocket targets Rust developers building web APIs and applications who want a framework with ergonomic request handling, built-in form validation, and managed state -- similar to Flask or Express but with Rust compile-time guarantees.
How it saves time or tokens
Rocket eliminates boilerplate through derive macros. Route handlers are plain functions annotated with #[get], #[post], etc. Request guards handle authentication and validation declaratively, so you do not write middleware chains manually. Type mismatches are caught at compile time.
How to use
- Add Rocket to your
Cargo.toml:
[dependencies]
rocket = "0.5"
serde = { version = "1", features = ["derive"] }
- Write route handlers as annotated functions.
- Mount routes and launch the server with
#[launch].
Example
#[macro_use] extern crate rocket;
use rocket::serde::{json::Json, Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
#[serde(crate = "rocket::serde")]
struct Task {
name: String,
done: bool,
}
#[get("/tasks")]
fn list() -> Json<Vec<Task>> {
Json(vec![
Task { name: 'Build API'.into(), done: false },
])
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![list])
}
Related on TokRepo
- Coding Tools -- Frameworks and libraries for building applications
- API Development Tools -- Tools for building and managing APIs
Common pitfalls
- Rocket 0.5 requires Rust nightly for some features. Check which features work on stable before committing to a toolchain.
- The
#[launch]macro sets up the async runtime. Do not create your own tokio runtime alongside it. - Rocket uses its own
serdere-export. Use#[serde(crate = "rocket::serde")]to avoid version conflicts with a top-level serde dependency.
常见问题
Rocket 0.5 works on stable Rust for core features. Some advanced features like certain proc macros may benefit from nightly. Check the Rocket changelog for the latest stable compatibility status.
Actix Web focuses on raw performance and uses an actor-based model. Rocket focuses on developer ergonomics with derive macros and compile-time route checking. Both are production-ready Rust web frameworks with different design philosophies.
A request guard is a type that implements the FromRequest trait. Rocket automatically validates and extracts data from incoming requests. Guards handle authentication, content negotiation, and custom validation declaratively.
Rocket 0.5 includes WebSocket support through the rocket_ws crate. It provides a typed API for WebSocket connections that integrates with Rocket request guards and managed state.
Yes. Rocket provides the FileServer handler for serving static files from a directory. Mount it at a path like rocket::fs::FileServer::from('static') and Rocket handles caching headers and MIME types.
引用来源 (3)
- Rocket GitHub— Rocket is a web framework for Rust focused on usability
- Rocket Official Site— Rocket documentation and guides
- Rust Async Book— Rust async runtime and web framework ecosystem
TokRepo 相关
讨论
相关资产
Xberg — Polyglot Document Intelligence Framework in Rust
A cross-language document extraction framework with a Rust core that parses PDFs, Office files, images, and 97+ formats into structured text and metadata.
Makepad — Creative Software Development Platform for Rust
Makepad is a Rust-based UI framework and live-coding IDE that compiles to WebGL, Metal, DirectX, and OpenGL. It combines a visual design environment with a high-performance rendering engine for building desktop, mobile, and web applications.
Semantic UI — Human-Friendly UI Component Framework
Semantic UI is a front-end component framework that uses human-friendly HTML class names to create responsive, themeable layouts. It provides over 50 UI components designed around natural language principles.
Hexo — Fast Node.js Blog Framework with Plugin Ecosystem
Hexo is a fast, simple, and extensible blog framework powered by Node.js. It renders Markdown posts into static HTML in seconds and supports hundreds of themes and plugins.