Actix Web
High-performance Rust web framework with async handlers, typed extractors, shared application state, and a mature middleware stack.
Official Documentation ↗4.18.1. Check the official docs for the latest API surface.Installation
cargo add actix-webcargo add serde --features derivecargo add serde_jsonMethods
No methods match this filter.
Actix Web is an async Rust web framework built on the actor model’s runtime but used through plain async functions. Handlers extract typed data from requests, return values that implement Responder, and run on a multi-threaded worker pool.
Overview
An application is an App with routes, middleware, and shared state, hosted by HttpServer across worker threads. Extractors such as web::Path, web::Query, and web::Json validate input at the type boundary. Handlers are async and return any Responder, including HttpResponse and web::Json. The framework also provides application state through web::Data and composable middleware through wrap.
Installation
Add actix-web with cargo add actix-web. Most applications also need serde with the derive feature for request and response types, and serde_json for dynamic payloads. The #[actix_web::main] macro requires the macros feature, which is enabled by default. Build with a current stable Rust toolchain and debug with RUST_LOG=info to see the logger middleware output.
Core API walkthrough
#[actix_web::main] starts the runtime and HttpServer::new builds an application per worker. #[get] and the other attribute macros register handlers with path templates. web::Json extracts JSON bodies and serializes responses. web::Data shares state behind an Arc across workers. wrap installs middleware such as logging and path normalization.
Pitfalls
The server factory runs once per worker, so captured state must be cloneable and cheap to clone. Blocking work inside an async handler stalls the worker; use web::block or a separate thread pool. Extractor order matters, and reading the body twice fails. Also remember that web::Path in the macros API yields a tuple struct, so call into_inner when a single value is needed.
Verification
Run cargo test with actix_web::test utilities, or start the server and curl each route. Confirm malformed JSON returns 400, a missing path segment returns 404, and the logger middleware prints a status line per request. Use cargo clippy to catch extractor and responder mistakes before deployment.
FAQ
- Do I need to add Tokio manually?
- No. Actix Web runs on its own actix-rt runtime built on Tokio, started by the #[actix_web::main] macro. Adding a second runtime or blocking the worker thread causes errors.
- Why does my handler fail with a trait bound error?
- Handlers must be async, take extractors that implement FromRequest, and return a type implementing Responder. Returning a Result requires both the Ok and Err types to implement Responder.
- How do I return JSON errors with the right status code?
- Return Result<T, actix_web::Error> and use helpers such as error::ErrorBadRequest, or implement ResponseError for a domain error type so actix-web maps it to a status and body.
Report a problem with this API docOpens a pre-filled issue on GitHub.