> rust-refactor

Architectural refactoring guide for Rust applications covering type safety, ownership patterns, error handling strategies, API design, project organization, module structure, naming conventions, conversion traits, and idiomatic patterns. Use when refactoring Rust codebases, reviewing PRs for architectural issues, improving type safety, designing error handling strategies, or organizing project structure. Complements the rust-optimise skill (performance patterns). Does NOT cover performance optim

fetch
$curl "https://skillshub.wtf/pproenca/dot-skills/rust-refactor?format=md"
SKILL.mdrust-refactor

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

CategoryImpactRulesKey Topics
Type Safety & PatternsCRITICAL20Newtypes, typestate builders, PhantomData, enums, trait objects, associated types
Ownership & BorrowingCRITICAL6Borrowing, Cow, lifetime elision, clone avoidance
Error HandlingHIGH15thiserror/anyhow, two-tier strategy, context, graceful degradation
API Design & TraitsHIGH6Sealed traits, extension traits, generic bounds, builder pattern
Project OrganizationHIGH6Cargo workspaces, crate separation, feature grouping
Module & VisibilityMEDIUM-HIGH9Re-exports, visibility control, test co-location, module splitting
Naming ConventionsMEDIUM-HIGH13RFC 430, snake_case, PascalCase, predicates, unit suffixes
Conversion TraitsMEDIUM5From/Into, AsRef, TryFrom, Deref
Idiomatic PatternsMEDIUM6Default, let-else, destructuring, match guards
Iterator & CollectionsLOW-MEDIUM5Iterator 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

┌ stats

installs/wk0
░░░░░░░░░░
github stars80
██████████
first seenMar 17, 2026
└────────────

┌ repo

pproenca/dot-skills
by pproenca
└────────────

┌ tags

└────────────