| name | rust-refactor |
| description | Rust refactoring patterns and techniques. Activates for code simplification, extract function/module, rename, dead code removal, and API redesign. Invoke with /rust-refactor. |
Rust Refactoring Skill
Refactoring Workflow
- Ensure tests pass before starting
- Make one change at a time — commit between steps
- Run tests after each change
- Use compiler as guide — let errors lead the refactor
Common Refactorings
Extract Function
Identify a block of code doing one thing and extract it:
fn process(data: &[u8]) -> Result<Output> {
}
fn process(data: &[u8]) -> Result<Output> {
let validated = validate(data)?;
let transformed = transform(&validated)?;
format_output(&transformed)
}
Replace Boolean Parameters with Enum
fn connect(host: &str, use_tls: bool) { ... }
enum Transport { Plaintext, Tls }
fn connect(host: &str, transport: Transport) { ... }
Replace Stringly-Typed Code with Newtypes
fn create_user(name: String, email: String, id: String) { ... }
struct UserName(String);
struct Email(String);
struct UserId(String);
fn create_user(name: UserName, email: Email, id: UserId) { ... }
Builder Pattern for Complex Construction
When a struct has many optional fields or complex initialization:
pub struct ServerConfig { ... }
pub struct ServerConfigBuilder {
port: u16,
host: String,
max_connections: Option<usize>,
}
impl ServerConfigBuilder {
pub fn new(port: u16) -> Self { ... }
pub fn host(mut self, host: impl Into<String>) -> Self { ... }
pub fn max_connections(mut self, n: usize) -> Self { ... }
pub fn build(self) -> ServerConfig { ... }
}
Convert match Chains to Method Dispatch
match shape {
Shape::Circle(r) => area_circle(r),
Shape::Rect(w, h) => area_rect(w, h),
}
impl Shape {
fn area(&self) -> f64 {
match self {
Self::Circle(r) => std::f64::consts::PI * r * r,
Self::Rect(w, h) => w * h,
}
}
}
Dead Code Detection
cargo check 2>&1 | grep "warning.*dead_code"
cargo udeps --all-targets
cargo unused-features analyze
API Simplification Checklist