Rust Refactoring Skill
Introduction
Use this skill when refactoring Rust code. It highlights Rust-specific code smells, suggests structural improvements, and provides a systematic workflow for safe, small refactors.
Instructions
- •Confirm intent: Identify the refactoring goal (performance, clarity, safety, modularity) and the target scope.
- •Scan for smells: Use the checklist in references to spot Rust-specific issues before changing code.
- •Prefer small, reversible steps: Split large refactors into tiny changes that keep behavior intact.
- •Structure-first refactors:
- •Keep modules cohesive; split by domain, not by type.
- •Prefer explicit ownership boundaries and minimal sharing.
- •Keep public APIs small and stable; hide internal details.
- •Ensure that refactorings do not increase the compelxity of the code.
- •Error handling:
- •Use domain-specific error types; avoid
Stringerrors in libraries. - •Prefer
thiserrorfor library errors andanyhowfor application-level contexts.
- •Use domain-specific error types; avoid
- •Performance and correctness:
- •Avoid needless clones; prefer borrowing and iterators.
- •Watch for
RefCellorRcusage where ownership can be simplified.
- •Document intent: Add small comments only for non-obvious invariants or safety assumptions.
- •Verify: Ensure tests cover the refactor and update or add tests if behavior changes.
References
- •references/smells-checklist.md: Rust code smells and structural checklist.
- •references/do-dont-examples.md: Do/don't refactoring examples.
Examples
- •Input: "Refactor a large module with mixed responsibilities."
Output: "Propose a split by domain, move shared types to a
typesmodule, and reduce public surface." - •Input: "Improve error handling in a parsing module."
Output: "Introduce a domain error enum and replace
Stringerrors with typed variants."