--- name: write-rust-current-edition description: House Rust style anchored to edition 2024 + MSRV 1.97.1 (as of 2026-07). Idioms, patterns, and version-guarded features. when_to_use: Writing any Rust code. Pin on rust_sdlc + backend + gpu coder roles. tags: [rust, style, versioned] --- # Writing Rust (edition 2024, MSRV 1.97.1) ## Toolchain we ship against Version fields as of **2026-07**. Verify with `rustc --version` before assuming. - **stable**: 1.97.1 - **workspace MSRV**: 1.97.1 (some crates require 1.98.0+; check `rust-toolchain.toml`) - **edition**: 2024 (default for new crates; workspace-wide bump per project) - **cargo tools we standardize on**: `cargo-nextest 0.9.140`, `cargo-audit 0.21+`, `cargo-llvm-cov 0.6+`, `cargo-machete`, `cargo-hakari` (for workspace unification) ## Idioms - **`let-else` over deep nesting.** ```rust let Some(user) = auth.load(id).await? else { return Err(NotFound.into()); }; ``` - **`?` with `.context()`** at every boundary (uses `anyhow::Context` or the equivalent). Bare `?` inside a private function is fine; at API surfaces, add context. - **`if let` chains** (stable in edition 2024): ```rust if let Some(x) = maybe && x > 0 && !seen.contains(&x) { ... } ``` Nest with `if let` blocks in edition-2021 crates until MSRV catches up. - **Struct-of-args when a fn exceeds ~5 params** — pass an `Args<'a>` struct. Clippy's `too_many_arguments` enforces at 7. - **`impl Trait` in argument position** for callbacks; `Box` only when erasure is genuinely needed. - **`Arc` over `Rc`** anywhere `Send` might matter — you're almost always in a Tokio runtime. ## Anti-patterns - **`unwrap()` in library code.** Only allowed in tests, in `main.rs` before the runtime spins up, and behind `debug_assert!`. - **`.clone()` as a borrow-checker escape.** Ask why the borrow can't work first. If it genuinely can't, add a comment explaining the tradeoff. - **`Arc>` when a channel would do.** For actor-like state, `tokio::sync::mpsc` is almost always the right answer. - **`Box>` in public APIs** where `impl Future` would do — leaks implementation and forces heap alloc. - **`#[allow(clippy::...)]` without a comment.** Every allow needs a one-line WHY. ## Testing - **`cargo nextest run`** as the default runner — faster, better output, isolates flaky tests. Fall back to `cargo test` when nextest isn't installed. - **`#[tokio::test]`** for async. Prefer `#[tokio::test(flavor = "multi_thread")]` when the code under test spawns. - See [[cargo-test-driven-development]] for the TDD flow. ## Lint policy Every Rust crate CI runs `cargo clippy --all-targets -- -D warnings`. Warnings ARE errors. Fix the warning; if it's a false positive, `#[allow(...)]` with a comment.