skills/pproenca/dot-skills/rust-refactor

rust-refactor

SKILL.md

Rust Refactor Best Practices

Architectural refactoring guide for Rust applications. Contains 91 rules across 10 categories, prioritized by impact from critical (type safety, ownership) to incremental (iterator idioms).

When to Apply

  • Refactoring existing Rust codebases or planning large-scale restructuring
  • Reviewing PRs for architectural issues and code smells
  • Designing type-safe APIs with proper error handling
  • Organizing Rust project structure with Cargo workspaces
  • Improving module boundaries and visibility control
  • Applying consistent naming conventions (RFC 430)
  • Replacing stringly-typed APIs with strong types

Rule Categories

Category Impact Rules Key Topics
Type Safety & Patterns CRITICAL 20 Newtypes, typestate builders, PhantomData, enums, trait objects, associated types
Ownership & Borrowing CRITICAL 6 Borrowing, Cow, lifetime elision, clone avoidance
Error Handling HIGH 15 thiserror/anyhow, two-tier strategy, context, graceful degradation
API Design & Traits HIGH 6 Sealed traits, extension traits, generic bounds, builder pattern
Project Organization HIGH 6 Cargo workspaces, crate separation, feature grouping
Module & Visibility MEDIUM-HIGH 9 Re-exports, visibility control, test co-location, module splitting
Naming Conventions MEDIUM-HIGH 13 RFC 430, snake_case, PascalCase, predicates, unit suffixes
Conversion Traits MEDIUM 5 From/Into, AsRef, TryFrom, Deref
Idiomatic Patterns MEDIUM 6 Default, let-else, destructuring, match guards
Iterator & Collections LOW-MEDIUM 5 Iterator methods, collect turbofish, filter_map

Quick Reference

Critical patterns — get these right first:

  • Use newtype patterns to prevent unit confusion and encode invariants
  • Prefer borrowing over ownership in function parameters
  • Use thiserror for library errors, anyhow for application errors
  • Use typestate builders for compile-time required field enforcement

Common mistakes — avoid these anti-patterns:

  • Stringly-typed APIs instead of strong types
  • Unnecessary clone calls when borrowing would work
  • panic! for recoverable errors instead of Result
  • Over-exposing internal types with pub visibility

Table of Contents

  1. Type Safety & PatternsCRITICAL
  2. Ownership & BorrowingCRITICAL
  3. Error HandlingHIGH
  4. API Design & TraitsHIGH
  5. Project OrganizationHIGH
  6. Module & VisibilityMEDIUM-HIGH
  7. Naming ConventionsMEDIUM-HIGH
  8. Conversion TraitsMEDIUM
  9. Idiomatic PatternsMEDIUM
  10. Iterator & CollectionsLOW-MEDIUM

References

  1. https://rust-lang.github.io/api-guidelines/
  2. https://rust-unofficial.github.io/patterns/
  3. https://doc.rust-lang.org/book/
  4. https://www.lurklurk.org/effective-rust/
  5. https://rust-lang.github.io/rust-clippy/

Related Skills

  • For performance optimization, see rust-optimise skill
Weekly Installs
40
GitHub Stars
71
First Seen
Feb 14, 2026
Installed on
codex36
github-copilot35
gemini-cli35
opencode33
kimi-cli33
cursor32