rust-idiomatic
Installation
SKILL.md
Rust Community Idiomatic Rust Best Practices
A structured guide to writing idiomatic Rust code based on patterns established by top contributors. Each rule includes incorrect/correct examples with realistic domain names and quantified impact metrics.
When to Apply
- Writing new Rust code in any project or crate
- Reviewing pull requests for idiomatic patterns
- Refactoring code that uses excessive cloning, manual loops, or opaque error handling
- Designing public APIs for libraries or shared modules
- Optimizing async code for correctness and cancellation safety
Rule Categories by Priority
| # | Category | Prefix | Impact | Rules |
|---|---|---|---|---|
| 1 | Ownership and Borrowing | own- | CRITICAL | 6 |
| 2 | Error Propagation | errprop- | CRITICAL | 6 |
| 3 | Type Safety | safe- | HIGH | 6 |
| 4 | Collections and Iterators | iter- | HIGH | 5 |
| 5 | Async Patterns | asyncp- | MEDIUM-HIGH | 5 |
| 6 | API Design | api- | MEDIUM | 5 |
| 7 | Serialization | serial- | MEDIUM | 4 |
| 8 | Performance | perf- | LOW-MEDIUM | 4 |
| Total | 41 |
Quick Reference
Ownership and Borrowing (CRITICAL)
own-borrow-over-clone- Borrow references instead of cloningown-cow-conditional- Use Cow for conditional ownershipown-arc-shared-state- Use Arc for shared async stateown-avoid-unnecessary-clone- Avoid unnecessary clone in closuresown-move-closures-spawn- Use move closures for tokio spawnown-into-conversions- Use Into conversions for ergonomic APIs
Error Propagation (CRITICAL)
errprop-question-mark- Use the question mark operator for error propagationerrprop-context-annotations- Add context to every fallible operationerrprop-custom-error-types- Define custom error types for domain boundarieserrprop-anyhow-applications- Use anyhow for application-level error handlingerrprop-thiserror-libraries- Use thiserror for library error definitionserrprop-boundary-conversion- Convert errors at module boundaries
Type Safety (HIGH)
safe-newtype-identifiers- Use newtype pattern for type-safe identifierssafe-enum-state-machines- Use enums to represent state machinessafe-builder-construction- Use builder pattern for complex object constructionsafe-from-into-traits- Implement From and Into for type conversionssafe-non-exhaustive- Use non_exhaustive on public enumssafe-phantom-data- Use PhantomData for type-level constraints
Collections and Iterators (HIGH)
iter-btreemap-determinism- Use BTreeMap for deterministic iterationiter-method-references- Use method references over closuresiter-collect-turbofish- Use turbofish on collect for clarityiter-chain-over-loops- Prefer iterator chaining over manual loopsiter-filter-map- Use filter_map instead of filter then map
Async Patterns (MEDIUM-HIGH)
asyncp-tokio-select- Use tokio select for concurrent branch waitingasyncp-pin-box-futures- Pin boxed futures to reduce async stack sizeasyncp-cancellation-safety- Design for cancellation safety in async codeasyncp-send-bounds- Ensure futures are Send for multi-threaded runtimesasyncp-structured-concurrency- Use JoinSet for structured concurrency
API Design (MEDIUM)
api-accept-asref- Accept impl AsRef for flexible string parametersapi-return-impl-iterator- Return impl Iterator over collected Vecapi-derive-default- Derive Default for configuration structsapi-display-user-facing- Implement Display for user-facing typesapi-sealed-traits- Use sealed traits for extension prevention
Serialization (MEDIUM)
serial-rename-all- Use rename_all for consistent wire formatserial-deny-unknown- Use deny_unknown_fields for strict deserializationserial-default-values- Use serde default for backward compatibilityserial-flatten-composition- Use serde flatten for struct composition
Performance (LOW-MEDIUM)
perf-preallocate-capacity- Pre-allocate Vec and String capacityperf-cow-avoid-alloc- Use Cow to avoid unnecessary allocationsperf-arc-str- Use Arc str over Arc String for shared stringsperf-lazy-lock- Use LazyLock for thread-safe lazy initialization
How to Use
- Before writing code: Review the CRITICAL rules (Ownership, Error Propagation) as they prevent the most impactful bugs and performance issues.
- During implementation: Follow Type Safety and Collections rules to leverage the compiler as a correctness tool.
- When designing APIs: Apply API Design and Serialization rules to create ergonomic, evolution-safe interfaces.
- When optimizing: Use Performance rules for targeted improvements in measured hot paths.
Reference Files
| File | Description |
|---|---|
references/_sections.md |
Section definitions and ordering |
references/own-*.md |
Ownership and borrowing rules (6 files) |
references/errprop-*.md |
Error propagation rules (6 files) |
references/safe-*.md |
Type safety rules (6 files) |
references/iter-*.md |
Collections and iterators rules (5 files) |
references/asyncp-*.md |
Async patterns rules (5 files) |
references/api-*.md |
API design rules (5 files) |
references/serial-*.md |
Serialization rules (4 files) |
references/perf-*.md |
Performance rules (4 files) |