Rust + axum — Claude Code rules
Cargo
- Rust 2024 edition.
clippy::pedanticallowed with focused#[allow(...)]near the call site. - One workspace crate per service. Shared types under
crates/<name>-types.
axum
Router::new()per module, composed atmain. No global mutable state.- Use
axum::extracttypes (State,Path,Query,Json) over manual deserialisation. - Handler return type is
Result<impl IntoResponse, AppError>whereAppError: IntoResponse. - Plug
tower_http::trace::TraceLayer+tower_http::cors::CorsLayerat the root.
Errors
- One application-level error enum via
thiserror. Variants map to HTTP status codes throughIntoResponse. anyhowis for bin/example crates only — never in library code that owns the error type.
Async
tokioas the runtime. No blocking I/O in async fns; wrap withtokio::task::spawn_blockingwhen needed.- Persistent connections live in
AppState(cloneableArc<…>interior).
Database
sqlxwith thepostgresfeature. Compile-time checked queries viasqlx::query!.- Migrations under
migrations/managed bysqlx-cli.