The practical way to use Codex on a Rust project is to run codex in the project root, keep a Rust-specific AGENTS.md, and work in small, test-verified steps. Codex reads compiler diagnostics and can iterate on borrow checker failures, but it still needs explicit cargo commands and boundaries. For a general CLI walkthrough, see How to use Codex CLI: a step-by-step guide.
Why Codex fits Rust development
Rust’s feedback loop is unusually useful for an agent. The compiler and clippy catch whole classes of errors before runtime, so Codex can run cargo check or cargo test and use the output to correct its own changes. Cargo gives a single, predictable entry point for builds and tests across crates and workspaces. Codex is also good at mechanical Rust work: deriving traits, threading lifetimes, converting error types, and adding unit tests around existing behavior. When Codex runs cargo test, it sees the same test names and failures you do, which makes conversations concrete. For workspaces, a single AGENTS.md at the repository root works for all crates, but you can also place one in each crate with crate-specific rules. The main risk is that it may try to satisfy the borrow checker by adding runtime overhead, so you should always review ownership changes. See Codex for how to start the agent.
Project setup: AGENTS.md for Rust
Create an AGENTS.md in the repository root and keep it short. List only commands and conventions that must be true for every task; do not paste entire documentation. Include the exact build, test, lint, and format commands Codex should run, workspace layout, and project-specific rules such as error handling and unsafe policy. A realistic Rust AGENTS.md looks like this:
# AGENTS.md
## Build and test
- Build: `cargo build --workspace`
- Test: `cargo test --workspace`
- Lint: `cargo clippy --workspace --all-targets --all-features -- -D warnings`
- Format check: `cargo fmt --all -- --check`
## Project rules
- Workspace crates: `crate/` is the library, `bin/` contains binaries.
- Do not edit `Cargo.lock` manually; run `cargo add` or update the manifest.
- Prefer owned data and explicit lifetimes over `Rc<RefCell<...>>`.
- `unsafe` requires a written safety comment explaining each invariant.
- Return errors through `thiserror` variants; do not add `unwrap` in library code.
- Before finishing a task, run `cargo test --workspace` and `cargo clippy --workspace --all-targets --all-features -- -D warnings`.
If the repository has feature flags, a custom toolchain, or a lint policy in rust-toolchain.toml, add those to the relevant lines. For smaller projects, replace --workspace with the crate name. These commands give Codex the exact validation loop it should follow instead of guessing.
A working loop for Rust changes
Plan the change first, then ask Codex for one small step. After each turn, run the tests yourself, read git diff, and only then ask for the next change. This keeps the agent from trying to fix a large refactor and a borrow checker issue at the same time. Example prompts that fit this loop:
- Add
#[derive(Debug, Clone, Serialize, Deserialize)]toConfigand write a round-trip test withserde_json. - Refactor
handle_connectionto avoid cloning theArc<Mutex<State>>in the hot path; show the ownership changes. - Add unit tests for
parse_headercovering empty input, missing colon, and UTF-8 header values. - Fix all clippy warnings in
src/parser.rswithout changing any public function signature. - Split the
runfunction insrc/main.rsintosetupandserve, then update the integration test to callsetupfirst.
After Codex makes changes, ask it to run cargo test --workspace; if that fails, paste the failing assertion or compiler error back and ask for a specific fix. Review the diff with git diff -- src/ and reject any unrelated formatting or dependency changes.
Rust tasks Codex handles and where to be careful
Codex handles mechanical and well-scoped Rust tasks reliably: trait derives, error type migration, test table generation, and replacing repeated code with a helper. It can also read a long compiler error and propose a lifetime adjustment. Where you need to watch closely is ownership and concurrency. Codex sometimes adds .clone() or Rc<RefCell<T>> to make the borrow checker pass, which changes performance and architecture. It may also suggest unsafe without a full soundness argument. Use the following table as a prompt and review reference.
| Rust task | Good prompt | Watch for |
|---|---|---|
| Derive traits | Add Serialize and Deserialize to Config with serde defaults | Changing field types just to satisfy serde |
| Ownership refactor | Remove the clone from handle_data and return a Cow<str> | Adding Rc<RefCell<T>> or leaking memory |
| Error handling | Replace unwrap in parse_config with thiserror variants | Mapping everything to anyhow::Error too broadly |
| Async code | Move blocking file read into tokio::task::spawn_blocking | Holding a MutexGuard across an await |
Also watch Cargo.toml and Cargo.lock: Codex may add a dependency instead of using a standard library or existing crate. In a workspace, tell it exactly which crate to edit; otherwise it may change the wrong Cargo.toml. For clippy, prefer -D warnings in CI, but let Codex run clippy without the -D first so it can iterate without failing every extra warning.
Using cargo test, clippy, and rustfmt for verification
Rust tooling makes verification explicit. Always require the agent to run cargo test --workspace before it says a task is done. For library code, ask for cargo test -p my_crate to keep the loop fast. Add cargo clippy --workspace --all-targets --all-features -- -D warnings and cargo fmt --all -- --check as final gates. Codex can run these commands, but you should re-run them in your own terminal and inspect git diff before committing. Treat compiler and clippy output as the contract: if the agent suppresses a warning with an attribute, ask why. For a second set of eyes, AI code review: check what your agents write shows how to review agent-written code automatically after each turn.
Running multiple Codex agents with AgentVera
AgentVera is a desktop app that runs Codex and other agents’ real command line tools side by side in one window, each in its own terminal panel. For Rust work, the three features that matter most are separate git worktrees per agent, automatic code review after each turn, and checkpoints. Worktrees let one agent refactor crate/ while another updates bin/ without overwriting files. A second model reviewing each turn catches borrow checker hacks such as needless clones or missing safety comments. Checkpoints let you roll back a bad lifetime refactor in one click and replay the session. For splitting Rust work across agents, AI agent workflow: teams, parallel parts and flows is a useful reference.
Wrap-up
Codex is most useful on Rust projects when you treat it as a pair programmer with strong compiler feedback. Set explicit cargo commands in AGENTS.md, keep tasks small, review ownership changes, and always run tests and clippy yourself. With that loop, you can offload repetitive Rust work without weakening the guarantees the language gives you.