Home › Rust › Rust Axum web API rules

Rust Axum web API rules

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

CursorRust

How to use

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

.cursor/rules/axum.mdcDownload  
---
description: Rust Axum web API conventions
globs: ["src/**/*.rs","Cargo.toml"]
alwaysApply: false
---

# Rust + Axum 0.8

- Router built in `app.rs` (`fn router(state: AppState) -> Router`) so tests can call it; `main.rs` only loads config, sets up tracing, and serves with `tokio::net::TcpListener` + `axum::serve`.
- Axum 0.8 path syntax: `/users/{id}` and `/files/{*path}` (not `:id`).
- Shared state in a `#[derive(Clone)] struct AppState` (pool, config) passed with `.with_state()`; extract via `State<AppState>`.
- Handlers take extractors (`Path`, `Query`, `Json`) and return `Result<impl IntoResponse, AppError>`. `AppError` is a `thiserror` enum implementing `IntoResponse` that maps variants to status codes and hides internal details.
- No `unwrap()`/`expect()` in request paths; use `?`.
- Database with `sqlx` (`query_as!` macros checked at compile time, migrations in `migrations/` run with `sqlx migrate run`). Keep `.sqlx/` offline data updated with `cargo sqlx prepare`.
- Validate input (e.g. `validator` crate or custom `TryFrom` newtypes) before use.
- Observability: `tracing` + `tower_http::trace::TraceLayer`; add timeouts and a body size limit via `tower_http` layers.
- Don't hold a lock (`Mutex`) across `.await`; use `tokio::sync` types where needed. CPU-heavy work goes in `spawn_blocking`.

## Done when
`cargo fmt --check`, `cargo clippy --all-targets -- -D warnings`, and `cargo test` pass. Handler tests use `tower::ServiceExt::oneshot` on the router.

More: all Rust rules · all Cursor rules

Related rules

Rust Actix Web rules

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

CursorRust

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