# Using Codex for Rust: a practical workflow guide

URL: https://agentvera.dev/en/blog/codex-for-rust/ · Published: 2026-11-01

Learn a practical Codex Rust workflow: project setup with AGENTS.md, borrow checker pitfalls, testing with cargo test, and multi-agent review.

**In short:**

- Set up AGENTS.md with cargo build, test, clippy, and rustfmt so Codex uses your project’s exact commands.
- Work in small, test-driven steps and review diffs; watch for borrow checker workarounds and unsafe code.
- Use AgentVera to run multiple Codex agents in separate worktrees with automatic review for parallel Rust changes.

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](/en/blog/how-to-use-codex-cli/).

## 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](/en/agents/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:

```markdown
# 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)]` to `Config` and write a round-trip test with `serde_json`.
- Refactor `handle_connection` to avoid cloning the `Arc<Mutex<State>>` in the hot path; show the ownership changes.
- Add unit tests for `parse_header` covering empty input, missing colon, and UTF-8 header values.
- Fix all clippy warnings in `src/parser.rs` without changing any public function signature.
- Split the `run` function in `src/main.rs` into `setup` and `serve`, then update the integration test to call `setup` first.

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](/en/blog/ai-code-review/) 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](/en/blog/ai-agent-workflow-pipeline/) 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.

## Frequently asked questions

**How do I use Codex with a Rust project?** Run codex in the project root and add an AGENTS.md file with cargo build, test, clippy, and fmt commands. Then ask for small, specific changes such as adding a trait impl or fixing borrow checker errors, and review the diff after each step.

**Does Codex understand Rust borrow checker errors?** Codex can read compiler output and often proposes fixes for ownership and lifetime issues. However, it may overuse clone, Rc, or RefCell to silence errors, so review those changes against your performance requirements.

**Can Codex run cargo test and clippy automatically?** Yes, when your AGENTS.md lists those commands, Codex can run them after making changes. Always confirm the terminal output and run cargo test --workspace yourself before merging.

**What should I put in AGENTS.md for Rust?** Include build, test, lint, and format commands; workspace layout; rules about Cargo.lock, unsafe, and error handling; and a requirement to run cargo test before finishing a task. Keep it short and project-specific.

---

**About AgentVera.** AgentVera is a desktop app for macOS, Windows and Linux, built by Solviera Teknoloji. It runs 18 AI coding agents (Claude Code, Codex, Gemini CLI, Copilot, Cursor, Antigravity, opencode, Hermes, Qwen Code, Kimi Code, Kiro, Amp, Droid, Goose, Vibe, Crush, Aider, Cline) side by side, per project, in real terminals with the user's own CLI installs and logins. The free plan has unlimited agents. Download: https://agentvera.dev/en/download/ · Pricing: https://agentvera.dev/en/pricing/
