1---2name: rust-standards3description: Write idiomatic Rust code. Use when writing Rust code.4---56## Testing78- Unit tests in `#[cfg(test)]` modules within each source file9- Integration tests in `tests/` directory1011## Naming1213- No `get_` prefix: `fn name()` not `fn get_name()`14- Iterator convention: `iter()` / `iter_mut()` / `into_iter()`15- Conversion naming: `as_` (cheap &), `to_` (expensive), `into_` (ownership)16- Static var prefix: `G_CONFIG` for `static`, no prefix for `const`1718## Data Types1920- Use newtypes: `struct Email(String)` for domain semantics21- Prefer slice patterns: `if let [first, .., last] = slice`22- Pre-allocate: `Vec::with_capacity()`, `String::with_capacity()`23- Avoid `Vec` abuse: use arrays for fixed sizes2425## Strings2627- Prefer `&str` over `String` in function parameters28- Use `String` for ownership: return `String` when transferring ownership29- Prefer bytes: `s.bytes()` over `s.chars()` when ASCII30- Use `Cow<str>` when you might need to modify borrowed data31- Use `format!` over string concatenation with `+`32- Avoid nested iteration: `contains()` on string is O(n\*m)3334## Error Handling3536- Use `?` for all fallible operations37- `unwrap()` in tests only, never production38- `expect()` only for provably impossible states; the message must justify why it can't fail e.g. `expect("regex is valid: validated at compile time")`39- `unwrap_or` / `unwrap_or_else` / `unwrap_or_default` for deliberate fallbacks40- Assertions for invariants: `assert!` at function entry4142## Memory4344- Meaningful lifetimes: `'src`, `'ctx` not just `'a`45- `try_borrow()` for RefCell to avoid panic46- Shadowing for transformation: `let x = x.parse()?`4748## Concurrency4950- Identify lock ordering to prevent deadlocks51- Atomics for primitives, not Mutex for bool/usize52- Choose memory order carefully: Relaxed/Acquire/Release/SeqCst5354## Async5556- Sync for CPU-bound; async is for I/O57- Don't hold locks across await: use scoped guards5859## Macros6061- Avoid unless necessary: prefer functions/generics62- Follow Rust syntax: macro input should look like Rust6364## Deprecated → Better6566- `lazy_static!` → `std::sync::OnceLock` (since 1.70)67- `once_cell::Lazy` → `std::sync::LazyLock` (since 1.80)68- `std::sync::mpsc` → `crossbeam::channel` only if you need multi-consumer or better performance under contention; `std::sync::mpsc` is fine for most use cases69- `failure`/`error-chain` → `thiserror`/`anyhow`70- `try!()` → `?` operator (since 2018)7172## Docs7374- All `pub` methods must have `///` doc comments75- All `///` doc comments must be extremely concise76- Imperative mood: `Returns the length` not `This function returns the length`77- Don't restate the name: `fn connect()` doesn't need "Connects to the server"78- Document the non-obvious: panics, errors, surprising edge cases; skip obvious params/returns79- No filler: no "This method...", "Note that...", "Please be aware..."80- `# Examples` only for non-trivial usage; doctests must compile and pass81- Don't write doctests purely for coverage; write them only when the example genuinely aids understanding8283## Quick Reference8485```86Naming: snake_case (fn/var), CamelCase (type), SCREAMING_CASE (const)87Format: rustfmt (just use it)88Docs: /// for public items, //! for module docs89Lint: #![deny(clippy::all, clippy::pedantic)]90```9192## Miscellaneous9394- Derive `Debug` on all public types; derive `Clone`, `PartialEq` only when needed95- Use clippy with `#![deny(clippy::all, clippy::pedantic)]` - fix all warnings96- No unsafe blocks97- Modules: one file per module, mod.rs only for re-exports