
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.
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.
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.
You Might Also Like
Related Skills

coding-agent
Run Codex CLI, Claude Code, OpenCode, or Pi Coding Agent via background process for programmatic control.
openclaw
add-uint-support
Add unsigned integer (uint) type support to PyTorch operators by updating AT_DISPATCH macros. Use when adding support for uint16, uint32, uint64 types to operators, kernels, or when user mentions enabling unsigned types, barebones unsigned types, or uint support.
pytorch
at-dispatch-v2
Convert PyTorch AT_DISPATCH macros to AT_DISPATCH_V2 format in ATen C++ code. Use when porting AT_DISPATCH_ALL_TYPES_AND*, AT_DISPATCH_FLOATING_TYPES*, or other dispatch macros to the new v2 API. For ATen kernel files, CUDA kernels, and native operator implementations.
pytorch
skill-writer
Guide users through creating Agent Skills for Claude Code. Use when the user wants to create, write, author, or design a new Skill, or needs help with SKILL.md files, frontmatter, or skill structure.
pytorch
implementing-jsc-classes-cpp
Implements JavaScript classes in C++ using JavaScriptCore. Use when creating new JS classes with C++ bindings, prototypes, or constructors.
oven-sh
implementing-jsc-classes-zig
Creates JavaScript classes using Bun's Zig bindings generator (.classes.ts). Use when implementing new JS APIs in Zig with JSC integration.
oven-sh