rust-idioms
43
总安装量
43
周安装量
#4855
全站排名
安装命令
npx skills add https://github.com/pproenca/dot-skills --skill rust-idioms
Agent 安装分布
claude-code
38
codex
35
gemini-cli
35
opencode
34
github-copilot
29
antigravity
27
Skill 文档
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.