Refactor existing Rust code while preserving behavior. Use when improving code structure, extracting functions, reducing duplication, introducing builders, or applying Martin Fowler patterns. Strangler fig for migrations. Keywords: refactor, extract, restructure, builder pattern, Martin Fowler, migration.
// Before
fn process(data: &Data) -> Result<Output> {
let validated = validate(data)?;
let transformed = transform(validated)?;
let output = finalize(transformed)?;
Ok(output)
}
// After
fn process(data: &Data) -> Result<Output> {
let validated = validate(data)?;
process_internal(validated)
}
// Before
let config = Config {
timeout: 100,
retries: 3,
debug: false,
};
// After
let config = Config::builder()
.with_timeout(100)
.with_retries(3)
.build();
const MAX_RETRIES: u32 = 3;
const DEFAULT_TIMEOUT_MS: u64 = 5000;
For large migrations:
cargo test --workspace
cargo clippy --workspace
git diff --stat # Should show minimal changes