Omniscio

OmniscioGuidesCLAUDE.md examplesRust

CLAUDE.md for a Rust project

Rust's conventions are checkable, and the ones an agent still gets wrong are the ones a compiler cannot see: where tests belong, which crate may open the database, and that the lockfile is not the file to fix a version conflict in.

So the file below spends its lines on exactly those things, and each block is explained underneath with the reason it earns a place. If you want the shape before the stack, the examples hub sets this file beside the other eight stack guides.

Checked against Anthropic's Claude Code documentation and the Rust toolchain's own documentation on November 25, 2026. By the Omniscio team. Updated .

The file

This one describes an HTTP service that splits its handlers, its domain rules and its database access into three crates. Save it as ./CLAUDE.md or ./.claude/CLAUDE.md at the workspace root, committed so the whole team reads the same file.

# ledgerd

axum HTTP service that ingests payment events and serves the ledger.
Rust 2024 edition, set in Cargo.toml; the toolchain channel is pinned in
rust-toolchain.toml. One workspace, three crates.

## Commands

cargo build --workspace
cargo run -p api
cargo test --workspace
cargo test -p core refund -- --nocapture
cargo fmt --all -- --check
CARGO_BUILD_WARNINGS=deny cargo clippy --workspace --all-targets

## Layout

- crates/api/       axum handlers and extractors; no business rules
- crates/core/      the ledger rules; no HTTP, no SQL, no runtime
- crates/store/     the only crate that opens a database connection
- crates/api/tests/ integration tests, one file per flow
- migrations/       append-only SQL, applied by the store crate

## Rules

- A recoverable failure returns Result. panic! is only for a broken invariant
  the caller cannot fix.
- .unwrap() and .expect() never appear outside tests and startup checks.
- Each crate owns one error enum and implements std::error::Error for it; the
  API maps it to a status code in one place.
- No unsafe block without a SAFETY comment naming the invariant it relies on.
- A function that only reads takes &str or &[T], not String or Vec<T>.
- A blocking file or database call never runs inside an async fn; it moves to
  spawn_blocking.
- A dependency version is declared once in the workspace manifest; a crate opts
  in without pinning a version of its own.

## Testing

- Unit tests sit next to the code in a #[cfg(test)] mod tests.
- Integration tests live in a crate's own tests/ directory, one file per flow.
  Each file is its own crate, so it imports the crate it tests and carries no
  #[cfg(test)].
- A helper shared by integration tests lives in tests/common/mod.rs, never
  directly in tests/.
- A bug fix starts with a test that fails before the fix.
- A doc example in a public function is a test; keep it compiling.

## Traps

- Cargo.lock belongs to Cargo. To bump a dependency run `cargo update`, never
  edit the lockfile, and commit the result.
- `cargo test` also runs doc tests, so one broken example in a doc comment fails
  the whole suite while every #[test] passes.
- Bare `cargo clippy` exits zero on warn-level lints. The CI line above denies
  warnings; run that one.

That is 56 lines. The cut was the same for every line: if an experienced Rust developer would learn it by opening the repository, it did not survive.

Why each block is there

The first two lines

The opening names the job, the edition and the toolchain because those decide what the code may look like. Cargo's manifest reference says the edition key applies to every target in the package, tests included, so the edition named here governs the tests Claude writes too.

The command block

Anthropic asks for instructions concrete enough to verify, and build commands are its own example of a fact worth holding in every session. Every command here is one somebody can paste into a shell and watch pass or fail.

The test line is broader than it looks. Cargo's own reference says cargo test compiles and executes unit, integration and documentation tests, and that everything after the two dashes goes to the test binary rather than to Cargo, which is why the narrow form is cargo test -p core refund -- --nocapture and not a flag in front of the dashes.

The last two commands are the ones an agent skips when it is in a hurry. rustfmt's README describes it as a tool for formatting Rust code according to style guidelines and gives --check an exit code, which is what makes it usable as a gate. Clippy's documentation says the easiest and most common way to run it is through cargo, that a bare run covers only the default lint group, and that CI is expected to elevate warnings to errors so the build fails.

The layout

The directory list carries the three crates and the one rule that is invisible in the tree: crates/core may not name HTTP or SQL. We put that rule in because, in our experience, an agent will otherwise put a database call in a handler, which is where it found one, and the domain rules quietly become untestable.

Anthropic's size target is what keeps the list honest. A tree of the whole workspace is the fastest way to spend 200 lines on something the agent could have read for itself.

The rules

The first rule uses Rust's own split rather than inventing one. The Rust Book groups errors into recoverable and unrecoverable, with Result<T, E> for the first and panic! for the second, so a session that reads the file knows which shape to return before it opens a single module.

The rest are the rules a reviewer has already had to write in a pull request: unwrap outside tests, one error enum per crate, a reference instead of an owned value, a blocking call outside the async runtime. Each is checkable in a diff, which is the test Anthropic asks a rule to pass.

Testing

The first two testing lines are the toolchain's convention, not ours. The Book says unit tests go in the src directory in each file, in a module named tests annotated #[cfg(test)], and that integration tests live in a top-level tests/ directory where each file is a separate crate that needs no such annotation.

The third line is the trap that convention creates. The Book shows a tests/common.rs helper being compiled and run as its own test target, empty, which is why the shared helper belongs in tests/common/mod.rs. The line after it, about a failing test before a fix, is this page's convention rather than the Book's: an agent will otherwise patch the symptom. And because doc tests run by default, the last line tells an agent that a doc example is compiled, not printed.

Traps

The lockfile line prevents the most common agent mistake with a generated file. The Cargo book says Cargo.lock holds the exact information about your dependencies, is maintained by Cargo and should not be manually edited, recommends checking it into version control when in doubt, and shows cargo update as the way to move a version.

The last line exists because the difference between a warning and a failure is the difference between a clean build and a drifted one. Clippy's own usage page says CI should elevate warnings to errors and, since Cargo 1.97, recommends the build.warnings option, set here through CARGO_BUILD_WARNINGS=deny, so a session that runs the bare command has done less than the team asked for.

What was left out, and why

  • The dependency list. It is in Cargo.toml, it changes, and a copy in the CLAUDE.md is stale the first time somebody adds a crate.
  • Lint settings and formatting options. They live in clippy.toml, rustfmt.toml and the lint attributes, where the tools enforce them. Repeating them here creates two sources of truth, and Anthropic warns that contradicting instructions may be followed arbitrarily.
  • A rule that matters in one crate. That is a path-scoped rule with paths frontmatter, which loads only when Claude works with matching files.
  • How to deploy or release. That is a multi-step procedure, and Anthropic's guidance is to put those in a skill rather than in context on every session.
  • A local database URL or a personal toolchain path. Those belong in CLAUDE.local.md, which you add to .gitignore.
  • "Write idiomatic Rust." Nobody can check it in a diff, and it spends context every turn.

Adapting it

Three swaps cover most Rust repositories. Change these and keep everything else.

  • A single crate instead of a workspace. Replace the -p and workspace commands with the plain ones, collapse the layout to src/, and keep the rules, testing and trap sections as they are.
  • A library instead of a service. Drop the handler, async and database lines, and add the one that matters for a library: what counts as a breaking change and which modules are public API. Doc tests become more important here, not less, because every example on a public item is compiled.
  • A different async runtime or none. Replace the handler line and the blocking-call rule with whatever your runtime documents, or delete both if the workspace is synchronous. Nothing else in the file depends on them.

Then prove it loaded before you trust it. /context all lists the memory files read at launch, and a file missing from that list is not being ignored; it is not being seen.

How this page was written

The guidance on what belongs in a CLAUDE.md, how long it should be and how Claude treats it was read from Anthropic's Claude Code memory documentation on November 25, 2026. The statements about cargo, the lockfile, doc tests, rustfmt and Clippy were read from the Rust project's own documentation the same day, and each one is linked in the Sources below.

The example file was written for this page and has not been run against a live repository. It is a starting point whose every line you are expected to check against your own workspace, which is what the adaptation steps above are for.

Sources

  • Anthropic's How Claude remembers your project, for what belongs in a CLAUDE.md, the size target, the concreteness rule and the conflict rule, read November 25, 2026.
  • The Cargo Book, Cargo.toml vs Cargo.lock, for what the lockfile is and whether to commit it, read November 25, 2026.
  • Cargo's cargo test reference, for what the command compiles and runs and where its arguments go, read November 25, 2026.
  • The Rust Book, Test Organization, for where unit and integration tests live, read November 25, 2026.
  • Clippy — Usage, for the cargo subcommand, the default lint group and denying warnings in CI, read November 25, 2026.
  • rustfmt, for what it is and what --check returns, read November 25, 2026.
  • The Cargo Reference, the manifest format, for the edition key, read November 25, 2026.
  • The Rust Book, Error Handling, for the recoverable and unrecoverable split, read November 25, 2026.

Common questions

What should a CLAUDE.md for a Rust project contain?

The cargo commands you actually run, the crate boundaries that are not obvious from the folder names, the error and unwrap policy, and the traps: the lockfile Cargo owns, doc tests that fail the suite, lints that only warn until CI denies them. Anthropic's target is under 200 lines, so anything Claude can read from Cargo.toml stays out.

Should I commit Cargo.lock?

Yes. The Cargo book says that when in doubt you should check Cargo.lock into version control, and that the file is maintained by Cargo and should not be edited by hand. It holds the exact versions of every dependency, which is what makes two machines build the same thing.

Why does cargo test fail on a doc comment?

Because cargo test compiles and runs documentation tests by default. Rustdoc extracts the code samples from a library's doc comments and executes them, so an example that no longer compiles fails the same command that runs your unit tests. Either fix the example or mark it as text rather than Rust.

Does this file work for a single-crate CLI?

The commands do; the layout does not. Drop the workspace paths, keep one src directory, and keep the unwrap, error and testing sections unchanged. A binary crate has no library target, so doc tests only exist if you also expose a lib.

Write CLAUDE.md once, and every agent reads it

Omniscio keeps your canonical CLAUDE.md or AGENTS.md mirrored to the filenames Codex, Gemini, Cursor and Copilot look for, and flags a copy that was edited outside it. Any model, any harness.

The features page covers what else it does, pricing covers the plans, and you can download it for Windows, macOS or Linux.