Rust Developer
Analyze, write, and fix Rust code iteratively until it compiles and passes tests.
When to Use
- Fix or diagnose Rust compilation errors
- Write new Rust functions, structs, traits, or modules
- Refactor Rust code for performance, safety, or idiomatic style
- Add or fix async/await code, ownership patterns, or lifetime annotations
- Lint and clean up Rust code with Clippy
Procedure
1. Understand the Task
- If the task lacks a specific file, error message, or function, ask one clarifying question before proceeding.
- Read the relevant source files before making changes
- Identify the exact error, goal, or refactor target
- Note ownership, lifetime, and async constraints in context
- Handle errors gracefully: if the task is too vague (e.g., 'fix my Rust code' with no file or error specified), ask for clarification.
2. Implement Changes
- Prefer idiomatic Rust: use
?for error propagation,impl Traitfor flexibility, iterators over manual loops - Follow ownership rules: minimize clones, prefer borrows, use
Arc/Mutexonly when shared mutability is necessary - Do not add new crate dependencies without asking. Prefer solutions using the standard library and existing dependencies.
- For async code: use
.awaitcorrectly, avoid blocking in async contexts, prefertokiopatterns already in the codebase - Match the existing code style and module conventions
3. Verify with cargo check
Run cargo check to catch compile errors quickly:
cargo check
- If Cargo commands cannot be executed in the environment, perform manual static analysis based on the code and clearly state that automated verification was not possible.
- If errors remain, read the compiler output carefully — Rust errors are precise
- Fix each error and re-run until clean
- For workspace crates, use
cargo check -p <crate-name>to target a specific crate
4. Lint with cargo clippy
cargo clippy
- Address
warninganderrorlevel lints - Use
#[allow(...)]sparingly and only with justification - Clippy suggestions are usually idiomatic improvements — apply them
5. Validate with cargo test
cargo test
- Run the full test suite; for a single crate:
cargo test -p <crate-name> - If tests fail, diagnose whether the fix broke existing behavior or the test expectations need updating
- Do not delete or skip tests to make them pass
6. Iterate Until Green
Repeat steps 3–5 until:
cargo checkexits with no errorscargo clippyshows no warnings or errors in the targeted crate(s) or touched filescargo testpasses all relevant tests
7. Explain Changes
After completing the fix, briefly summarize:
- What was wrong or missing
- What pattern or Rust feature was applied
- Any trade-offs made (e.g., cloning vs. restructuring lifetimes)
Idiomatic Rust Patterns
| Goal | Preferred Pattern |
|---|---|
| Error handling | Result<T, E> + ? operator |
| Optional values | Option<T> + map/and_then |
| Shared ownership | Arc<T> (thread-safe), Rc<T> (single-thread) |
| Interior mutability | Mutex<T>, RwLock<T>, Cell<T>, RefCell<T> |
| Async runtime | tokio::spawn, .await, async fn |
| Collections | Iterators + collect() over manual loops |
| Newtype pattern | Wrap primitives for type safety |
| Builder pattern | For structs with many optional fields |
Common Compiler Error Fixes
- E0382 use of moved value: borrow instead of move, or clone if ownership must be transferred
- E0502 conflicting borrows: restructure to avoid overlapping mutable/immutable borrows
- E0277 trait not implemented: add a
wherebound or implement the trait - E0106 missing lifetime specifier: introduce lifetime annotations or use owned types
- E0716 temporary dropped while borrowed: extend the lifetime of the temporary with a
letbinding
Quality Checklist
-
cargo checkpasses with no errors -
cargo clippyintroduces no new warnings -
cargo testpasses all tests - No unnecessary
clone(),unwrap(), orexpect()in production paths - Async code does not block the executor
- Public APIs have doc comments if the codebase uses them
Source: kowito/chopin — distributed by TomeVault.