Agent skill
rust-idioms
Rust refactoring and idiomatic patterns guidelines from the Rust Community (formerly rust-refactor). This skill should be used when writing, reviewing, or refactoring Rust code to ensure idiomatic patterns and clean architecture. Triggers on tasks involving Rust types, ownership, error handling, traits, modules, conversions, or iterator patterns.
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/data/rust-idioms
SKILL.md
Rust Community Rust Refactoring Best Practices
Comprehensive refactoring and idiomatic patterns guide for Rust applications, maintained by the Rust Community. Contains 44 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
When to Apply
Reference these guidelines when:
- Writing new Rust code with strong type guarantees
- Refactoring ownership and borrowing patterns
- Designing error handling strategies
- Creating public APIs with traits and generics
- Organizing modules and controlling visibility
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Type Safety & Newtype Patterns | CRITICAL | type- |
| 2 | Ownership & Borrowing | CRITICAL | own- |
| 3 | Error Handling Patterns | HIGH | err- |
| 4 | API Design & Traits | HIGH | api- |
| 5 | Module & Visibility | MEDIUM-HIGH | mod- |
| 6 | Conversion Traits | MEDIUM | conv- |
| 7 | Idiomatic Patterns | MEDIUM | idiom- |
| 8 | Iterator & Collections | LOW-MEDIUM | iter- |
Quick Reference
1. Type Safety & Newtype Patterns (CRITICAL)
type-newtype-units- Use newtype pattern for unit safetytype-newtype-invariants- Encode invariants in newtype constructorstype-non-exhaustive-enums- Use non-exhaustive for extensible enumstype-phantom-data- Use PhantomData for type-level statetype-strong-typing-strings- Replace stringly-typed APIs with strong typestype-builder-required-fields- Use typestate builders for required fields
2. Ownership & Borrowing (CRITICAL)
own-prefer-borrowing- Prefer borrowing over ownership in parametersown-cow-conditional-clone- Use Cow for conditional ownershipown-accept-borrowed-types- Accept borrowed types over owned referencesown-return-owned-for-flexibility- Return owned types for caller flexibilityown-avoid-unnecessary-clone- Avoid unnecessary clone callsown-lifetime-elision- Leverage lifetime elision rules
3. Error Handling Patterns (HIGH)
err-use-result-not-panic- Use Result instead of panic! for recoverable errorserr-thiserror-for-libraries- Use thiserror for library error typeserr-anyhow-for-applications- Use anyhow for application error handlingerr-question-mark-propagation- Use the question mark operator for error propagationerr-option-for-absence- Use Option for absence, not sentinel values
4. API Design & Traits (HIGH)
api-derive-common-traits- Derive common traits for public typesapi-impl-standard-traits- Implement standard traits for ergonomic APIsapi-generic-bounds- Use trait bounds for generic flexibilityapi-sealed-traits- Use sealed traits to prevent external implementationapi-builder-pattern- Use builder pattern for complex constructionapi-extension-traits- Use extension traits to add methods to foreign types
5. Module & Visibility (MEDIUM-HIGH)
mod-minimize-pub-api- Minimize public API surfacemod-pub-use-reexports- Use pub use for clean module re-exportsmod-split-large-modules- Split large modules into submodulesmod-crate-prefix-imports- Use crate:: prefix for internal importsmod-tests-submodule- Use tests submodule for unit tests
6. Conversion Traits (MEDIUM)
conv-impl-from-not-into- Implement From instead of Intoconv-asref-for-flexibility- Accept AsRef for flexible string parametersconv-impl-deref-for-newtypes- Implement Deref for transparent newtype accessconv-tryfrom-for-fallible- Use TryFrom for fallible conversionsconv-inner-function-pattern- Use inner function pattern to reduce monomorphization
7. Idiomatic Patterns (MEDIUM)
idiom-default-trait- Implement Default instead of new() without argumentsidiom-constructor-naming- Follow constructor naming conventionsidiom-let-else- Use let-else for early returns on pattern match failureidiom-struct-update-syntax- Use struct update syntax for partial overridesidiom-destructuring-assignment- Use destructuring for multiple returns and field accessidiom-match-guards- Use match guards for complex conditions
8. Iterator & Collections (LOW-MEDIUM)
iter-prefer-iterators-over-loops- Prefer iterator methods over manual loopsiter-use-collect-turbofish- Use turbofish for explicit collect typeiter-filter-map-combined- Use filter_map for combined filter and transformiter-avoid-collect-then-iterate- Avoid collecting then iteratingiter-enumerate-for-indices- Use enumerate instead of manual index tracking
How to Use
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
Full Compiled Document
For a single-file comprehensive guide, see AGENTS.md.
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?