API Reference

Rocket

Ergonomic web framework for Rust with type-safe routing, request guards, fairings and built-in testing.

Official Documentation ↗
CompatibilityThis documentation covers version rocket 0.5 / Rust 1.64+(需 nightly 的部分特性在稳定版已放宽). Check the official docs for the latest API surface.

Installation

cargocargo add rocket

Methods

Overview

Rocket is a web framework that emphasizes developer ergonomics and type safety. Routes are annotated functions, request guards validate inputs, and fairings provide middleware.

Core Methods

Annotate handlers with route attributes, build the application, mount routes and manage state, then launch. Path parameters are typed, and handlers return responders such as Json or a status type.

Usage Notes

Use request guards for auth and validation, manage shared state explicitly, and write integration tests with the local client. Configure the environment per deployment stage.

Common Pitfalls

Some features historically required nightly, so check the version requirements. Forgetting a guard lets unauthenticated requests through. State must be managed before routes using it are mounted.

FAQ

What are request guards?
Types implementing FromRequest can validate and extract data from a request, such as parsing a token or requiring authentication, and handlers take them as arguments.
What are fairings?
Fairings are middleware-like hooks that run on every request or response, useful for logging, headers or database connection setup, with on_attach, on_request and on_response callbacks.
How do I test a Rocket app?
The local Client type dispatches requests against the app in tests, so you can assert status codes and bodies without binding a port.