Rust + Axum + PostgreSQL API (sqlx & Tokio)
Rust API and backend rules: Axum 0.8 on Tokio, compile-time SQL checks with sqlx 0.9, Tower middleware, typed errors and a cargo check loop for AI agents.
- Formats
- 4 files
- AGENTS.md
- 50 lines
- CLAUDE.md
- 18 lines
- Languages
- Rust
- Updated
- Oct 2026
- Used by
- 4 projects
Writes .claude/skills/rust-axum-postgres/SKILL.md
$ curl -s --create-dirs -o .claude/skills/rust-axum-postgres/SKILL.md https://stackitfast.com/rules/rust-axum-postgres/SKILL.mdRule files
Works with Cursor · Claude Code · Windsurf · AGY
Architecture notes
Architecture Overview
High-throughput systems architecture using Rust, Axum, Tokio, and sqlx with compile-time verified database operations.
Key Advantages
- Zero Memory Leaks & Zero GC Pauses: Memory safety guaranteed at compile time without garbage collector spikes.
- Compile-Time Query Verification:
sqlxchecks SQL queries against PostgreSQL schemas duringcargo build, preventing runtime syntax errors. - Massive Concurrency: Tokio lightweight asynchronous tasks handle tens of thousands of concurrent connections on minimal hardware.
Related Rust stacks
Building pages rather than an API? See Axum + htmx + Askama for server-rendered HTML, Leptos full-stack for a Rust UI, Loco for Rails-style conventions and Tauri + React for desktop apps. What open-source Rust projects actually use: Building web apps in Rust in 2026.
Frequently asked questions
Does this AGENTS.md work with Cursor, Claude Code, and Windsurf?
Yes — AGENTS.md is the open, cross-tool standard read by Cursor, Claude Code, Windsurf, and 30+ other agents. A dedicated .mdc file is also included for Cursor's native .cursor/rules format.
Why does sqlx need a .sqlx/ offline query cache?
sqlx's query_as! and query! macros connect to a live database at compile time to verify SQL syntax and types, which breaks CI environments that don't have database access. cargo sqlx prepare snapshots that verification into a .sqlx/ cache file that ships in the repo, so cargo build can compile-check queries offline using the cached metadata instead of a live connection.
Why is unwrap() banned in route handlers?
unwrap() panics the current thread on an Err or None, and in an Axum handler that means the request-handling task crashes — potentially with a raw error message leaking to the client and no structured logging around the failure. Propagating errors with the ? operator into a typed AppError that implements IntoResponse guarantees every failure path returns a consistent, controlled HTTP response instead.
Why does Rust work well with AI coding agents?
Most mistakes an agent makes in Rust surface as compiler errors with a line number and a suggested fix: type mismatches, unhandled enum variants, borrow conflicts and, with sqlx, SQL that does not match the schema. A loop of cargo check, cargo clippy -- -D warnings and cargo test after every change lets the agent correct itself before anything runs.
Is this rule for an API or a full web app?
It targets JSON APIs and backend services with a separate frontend. For server-rendered pages use the Axum + htmx + Askama rule, for a full-stack Rust UI the Leptos rule, and for Rails-style conventions the Loco rule.
Used in production
Explore all stacksUpleveler
1Upleveler keeps every log, note and goal as plain local files and routes all AI work through a local Ollama model in small map-reduce prompts, so one Rust binary can serve the CLI, a Ratatui app and a 127.0.0.1 Axum dashboard without an account or cloud.
RustFS
1RustFS splits a MinIO-style object store into dozens of single-purpose Rust crates behind one binary, keeps extra protocols behind cargo features, and moves inter-node traffic to gRPC, so storage, healing and IAM evolve separately.
SeaweedFS
1Decoupling directory metadata from physical volume storage eliminates central metadata bottlenecks, enabling massive horizontal scaling for small and large files alike.
Axum
Axum skips a custom middleware system and treats every route and layer as a Tower service, so it inherits timeouts, tracing, compression and auth from tower-http and shares middleware with Hyper and tonic code.