Built from apollographql/rust-best-practices (Apache-2.0): its rules file as SKILL.md and the handbook chapters as references. Used for the API and the website server.
6.3 KiB
6.3 KiB
name, description
| name | description |
|---|---|
| rust-best-practices | Rules for writing and reviewing idiomatic Rust (ownership, Option/Result flow, errors, iterators, clippy, testing, docs, dispatch). Use whenever writing, changing or reviewing Rust code. Handbook chapters with examples are in references/. |
Rust best practices
Apply these rules when writing or reviewing Rust. The handbook chapters in references/ explain each rule with examples; read the matching chapter when a rule is unclear.
Rule 1: Borrow by default
- Default to borrowing (
&T,&mut T) and avoid cloning. - Use owned inputs only when semantics or API require it.
- Public APIs should prefer
&str/&[T]overString/Vec<T>unless ownership is needed. - Prefer
if/else if/elseover early-return-heavy control flow when that improves readability.
Rule 2: Copy/value semantics intentionally
- Pass small
Copytypes by value. - Avoid passing large structs/heap-backed values by value in public APIs.
- Do not mark large complex types as
Copy.
Rule 3: Option/Result control flow
- Prefer
let PATTERN = EXPR else { ... }for early expected exits. - Use
if let ... elsewhen extra logic is required in both branches. - Use
?for normal propagation. - Keep
unwrap/expectrestricted to tests or impossibility proofs. - Avoid silent recovery from
Err; preserve root-cause visibility where recovery is needed. - In production fallible paths, return
Resultwith meaningful context instead of aborting. - If
expectis used, include invariant-focused messages.
Rule 4: Prevent eager allocation
- Use
_elsemethods for costly defaults. - Defer conversions and allocations until consumed.
- Pass iterators through pipelines instead of collecting then reprocessing.
- Use
_or_elseto delay expensive default creation.
Rule 5: Iterator and loop policy
- Prefer iterator chains for pure transformations.
- Use
forwhen requiringbreak,continue,return, or complex side effects. - Keep transform chains readable and line-broken.
Rule 6: Imports and style
- Keep import grouping: std/core/alloc, external crates, workspace,
super::,crate::. - Use stable rustfmt ordering where available.
- Keep side effects at system boundaries; keep transformation logic side-effect free.
- Keep transform chains readable and line-broken.
- Prefer functional composition for pure logic; use mutable/shared state only at boundaries.
Rule 7: Clippy-first workflow
- Use:
cargo clippy --all-targets --all-feature --locked -- -D warnings. - Treat clippy warnings as actionable.
- Avoid blanket suppression; if necessary, prefer documented local
#[expect(...)].
Rule 8: Performance only after measurement
- Don't optimize by intuition.
- Measure with release builds, profiling, and benchmarks.
- Prefer measured alternatives when optimization claim is made.
- Performance changes should be backed by evidence.
Rule 9: Error strategy
- Library crates: prefer typed errors (
thiserror) and crate-level variants. - Binaries:
anyhowacceptable for ergonomics, with consistent context. - Preserve error context where callers need it.
- Avoid silent recovery from
Errunless intentionally handled. - Prefer explicit boundary conversions for typed errors.
Rule 10: Public API docs
- Document public items with
///and include:- what it does,
- parameters and outputs,
- errors, panics, and safety notes when relevant,
- examples.
- Use
//!for module/crate-level purpose. - Keep comments for intent, assumptions, constraints, or safety rationale; remove stale comments.
Rule 11: Testing quality
- One test should validate one behavior.
- Use descriptive sentence-like names.
- Use both unit and integration coverage for behavioral and contract verification.
- Use snapshot testing for complex structured outputs only.
- Required quality gates:
cargo fmt --all --checkcargo clippy --workspace --all-targets --all-features -- -D warningscargo test --workspace --all-features
- Pre-merge checklist:
- intentional ownership/data-flow review
- explicit and aligned error handling
- measured performance claim verification
- documentation reflects runtime behavior
Rule 12: Dispatch choice
- Prefer static dispatch (
impl Trait/ generic bounds) when types are known and performance matters. - Use dynamic dispatch (
dyn Trait) for plugin-style/runtime polymorphism needs. - Delay boxing until required by design boundaries.
Rule 13: Type-state when valuable
- Use type-state when illegal states should be compile-time errors.
- Skip type-state for simple enums/flags where runtime checks are clearer.
Rule 14: Comments and TODO policy
- Keep comments for intent, constraints, safety, or architecture rationale.
- Remove stale comments and avoid obvious-restatement comments.
- Convert TODOs into issue references:
// TODO(issue #NNN): ... - Do not add comments for explicit code changes unless required for intent or safety.
- Avoid unjustified
#[allow]; prefer local#[expect(...)]only when explicitly needed.
Rule 15: Pointer and concurrency model
- Prefer references over pointers.
- Use
Arc/Rconly by threading requirement. - Use
Cell/RefCellonly when borrow rules must be relaxed. - Treat raw pointer usage as unsafe API surface requiring explicit invariants.
- Default to borrowing (
&T,&mut T) and avoid cloning unless ownership is required. - Use small
Copytypes by value and pass heap-backed or large types by reference. - Prefer
From/Intoover manual bit-level conversions.
Rule 16: Workflow
- Never "clean up", normalize, or replace in-progress experiments unless explicitly asked.
- Check
git status --short --branchbefore starting work. - Prefer explicitness over magic; extract identifier/key strings into named constants.
- Keep code paths simple and explicit before adding clever abstractions.
- Never introduce new
panic!,unwrap, orexpectin shipping code unless explicitly approved.
Extra rules
- Avoid:
- unnecessary cloning in hot paths
- unjustified
#[allow] - missing context on errors
- copying large values by value without justification
- TODOs without ownership/context