Home › Rust › Rust Actix Web rules

Rust Actix Web rules

Cursor rules for Actix Web 4 services in Rust: app state, extractors, ResponseError, middleware, and integration tests.

CursorRust

How to use

Save this as .cursor/rules/actix.mdc in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

Note: Review and test before use. Provided as is, without warranty. See Terms.

.cursor/rules/actix.mdcDownload  
---
description: Rust Actix Web conventions
globs: ["src/**/*.rs"]
alwaysApply: false
---

# Actix Web 4

- `main.rs` loads config, builds shared state, and runs `HttpServer::new(move || App::new().configure(routes::config).app_data(state.clone()))`. Keep route registration in `routes::config(cfg: &mut web::ServiceConfig)` so tests reuse it.
- Shared state is `web::Data<AppState>` (it's an `Arc`), created once outside the closure. Don't build DB pools inside the factory closure (it runs per worker).
- Handlers use extractors (`web::Path<(Uuid,)>`, `web::Query<Params>`, `web::Json<CreateOrder>`) and return `Result<impl Responder, AppError>`.
- `AppError` is a `thiserror` enum implementing `ResponseError` (`status_code` + `error_response` with a JSON body). Hide internal error details.
- Limit payloads: `web::JsonConfig::default().limit(64 * 1024)` with a custom error handler for clean 400s.
- Middleware: `Logger`/`tracing-actix-web`, `Compress`, CORS (`actix-cors` with explicit origins). Auth as an extractor (`FromRequest` for `AuthUser`) rather than checks in every handler.
- Blocking work (CPU, sync libraries) in `web::block` or `spawn_blocking`.
- DB via `sqlx` with a `PgPool` in state; queries take `&pool`.
- No `unwrap()` in handlers.

## Done when
`cargo fmt --check`, `cargo clippy -- -D warnings`, and `cargo test` pass; handler tests use `actix_web::test::init_service` + `TestRequest`.

More: all Rust rules · all Cursor rules

Related rules

Rust async (Tokio) .claude/rules file

A path-scoped Claude Code rules file for async Rust on Tokio: no blocking, cancellation safety, locks, and tasks.

Claude Code (CLAUDE.md)Rust

Rust Axum web API rules

Cursor rules for Rust web services with Axum 0.8, Tokio, sqlx, typed errors, and tracing.

CursorRust

Rust CLI CLAUDE.md

CLAUDE.md for Rust command-line tools built with clap, anyhow and thiserror.

Claude Code (CLAUDE.md)Rust

Rust library crate AGENTS.md

AGENTS.md for publishable Rust library crates: API design, error types, features, MSRV, docs, and semver checks.

AGENTS.mdRust

Cloudflare Agents SDK rules

Cursor rules for stateful AI agents on Cloudflare (agents package): Durable Object state, SQL, scheduling, and WebSockets.

CursorCloudflare Workers / Hono

Django Ninja API rules

Cursor rules for Django Ninja APIs on Django 5.2: routers, Pydantic schemas, auth, async views, and tests.

CursorDjango

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.