Skills2026年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.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
step-1.md
直接安装命令
npx -y tokrepo@latest install 412ff08d-3634-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

TL;DR
Rocket is a Rust web framework that uses derive macros and type-safe routing for fast, secure web applications.
§01

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.

§02

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.

§03

How to use

  1. Add Rocket to your Cargo.toml:
[dependencies]
rocket = "0.5"
serde = { version = "1", features = ["derive"] }
  1. Write route handlers as annotated functions.
  1. Mount routes and launch the server with #[launch].
§04

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])
}
§05

Related on TokRepo

§06

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 serde re-export. Use #[serde(crate = "rocket::serde")] to avoid version conflicts with a top-level serde dependency.

常见问题

Does Rocket require Rust nightly?+

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.

How does Rocket compare to Actix Web?+

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.

What is a request guard in Rocket?+

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.

Does Rocket support WebSockets?+

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.

Can Rocket serve static files?+

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)

讨论

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

相关资产