| description | Rust patterns and idioms for .rs files | |
|---|---|---|
| globs |
|
- Use
Result<T, E>for all recoverable errors. Do not useunwrap()orexpect()in library code. - Use the
?operator to propagate errors rather than matching on everyResultmanually. - Define custom error types that implement
std::fmt::Displayandstd::error::Error. - Use
thiserrorfor library errors andanyhowfor application-level error handling. - Reserve
panic!for unrecoverable programmer errors (e.g., violated invariants).
- Prefer
&stroverStringfor function parameters that only need to read a string. - Prefer
&[T]over&Vec<T>for function parameters that only need to read a slice. - Return owned types (
String,Vec<T>) from functions that create new data. - Minimize clone calls. If you need to clone frequently, reconsider the ownership design.
- Use
derivemacros (Debug,Clone,PartialEq,serde::Serialize) whenever possible. - Implement
Displayfor types that will appear in user-facing output. - Prefer newtype wrappers over raw primitives for domain values (e.g.,
struct UserId(u64)). - Use iterators and iterator adapters (
map,filter,collect) instead of manual loops.
- Minimize
unsafeblocks. Encapsulate unsafe code in a small, well-documented module. - Every
unsafeblock must have a comment explaining the invariant that makes it sound.
- Follow all
clippyrecommendations. Treat clippy warnings as errors in CI. - Run
rustfmton all files. Do not manually format code that rustfmt would reformat. - Use
cargo testfor unit and integration tests. Place unit tests in a#[cfg(test)]module at the bottom of the source file.