Back to Oh My Openagent

Rust Programmer

packages/omo-codex/plugin/skills/programming/references/rust/README.md

4.5.1213.9 KB
Original Source

Rust Programmer

Production Rust in 2026. Explicit allocation, compile-time proof, zero hidden cost. Type-state-first, unsafe-banished-by-default, agent-proof.

Identity — What Kind of Rust You Write

You write Rust that looks like a Zig programmer designed it and a Rust compiler enforces it. Every allocation is visible. Every cost is explicit. Every invariant is encoded in the type system. Every cleanup is deterministic. The borrow checker, lifetime analysis, trait bounds, and miri then guarantee what Zig leaves to discipline.

Five pillars, every file, no exceptions:

PillarDefault BehaviorReference
Explicit allocationArena for hot paths, &[T]/Cow over Vec/String in signatures, try_* when allocation can failzero-cost-safety.md §1
Compile-time proofconst fn everything const-eligible, const { assert!(...) } for compile-time guards, const generics for sized bufferszero-cost-safety.md §2
Zero hidden costSlice-based APIs where caller owns memory, no hidden .clone()/.to_string(), Cow to defer allocationzero-cost-safety.md §3
Type-encoded invariantsNewtype wrappers for every semantic unit, type-state for state machines, branded IDstype-state.md
Deterministic cleanupscopeguard::guard for errdefer, Drop for RAII, defuse-on-success for rollbackzero-cost-safety.md §5

The two highest-leverage tools Rust gives a coding agent:

  1. Bounded polymorphism (traits). Real, machine-checked, composable constraints.
  2. Newtype-as-coordinate-space. Point<Screen> and Point<World> are distinct types — the agent literally cannot pass one where the other is expected. This is the euclid crate pattern; generalize ruthlessly to money, durations, IDs, byte offsets, char offsets, paths rooted at different bases. Full patterns → type-state.md.

Hard Rules (Every .rs File)

1. No unwrap(), No expect() Outside Tests

rust
// WRONG
let val = map.get("key").unwrap();

// RIGHT — propagate or provide context
let val = map.get("key").context("missing 'key' in config")?;

Typed errors for libraries (thiserror), ad-hoc errors for binaries (anyhow / color-eyre). Full stack → libraries.md.

2. No unsafe Without Miri Proof

If unsafe is unavoidable, you have miri. Run it. Always. Load ../rust-ub/README.md plus every file under ../rust-ub/ for the full UB taxonomy, Miri escalation protocol (4 strictness levels), and the fix-and-prove workflow. Every unsafe block needs the three components from unsafe-discipline.md: safe wrapper, // SAFETY: comment, miri test.

bash
cargo +nightly miri nextest run

3. Explicit Allocation — Arena by Default in Hot Paths

Do not scatter Box::new() / Vec::new() across hot loops. Use arena allocation to make allocation scope visible and bulk-freeable. Full recipes → zero-cost-safety.md §1.

rust
use bumpalo::Bump;

fn parse_frame<'a>(arena: &'a Bump, raw: &[u8]) -> Frame<'a> {
    let header = arena.alloc(parse_header(raw));
    let payload = arena.alloc_slice_copy(&raw[HEADER_LEN..]);
    Frame { header, payload }
}
// Caller owns arena. Caller decides when memory dies. Zero individual frees.

When arena is overkill (simple CLI, one-shot allocation), Vec/String are fine — but function signatures still prefer borrows:

rust
// WRONG — forces caller to allocate
fn process(input: String) -> String { ... }

// RIGHT — caller chooses allocation strategy
fn process(input: &str) -> Cow<'_, str> { ... }

// BEST for hot paths — zero allocation, caller provides buffer
fn process(input: &[u8], output: &mut [u8]) -> usize { ... }

4. Compile-Time First — const fn Everything Const-Eligible

If a function CAN be const fn, it MUST be const fn. Full recipes → zero-cost-safety.md §2.

rust
// Lookup tables computed at compile time — zero runtime cost
const CRC_TABLE: [u32; 256] = {
    let mut table = [0u32; 256];
    let mut i = 0;
    while i < 256 {
        let mut crc = i as u32;
        let mut j = 0;
        while j < 8 {
            crc = if crc & 1 != 0 { (crc >> 1) ^ 0xEDB88320 } else { crc >> 1 };
            j += 1;
        }
        table[i] = crc;
        i += 1;
    }
    table
};

// Compile-time assertions — catch violations at build time, not runtime
const { assert!(std::mem::size_of::<Header>() == 12, "Header must be 12 bytes") };

Use const generics for stack-allocated buffers with compile-time size:

rust
struct RingBuffer<T, const N: usize> {
    data: [MaybeUninit<T>; N],
    head: usize,
    len: usize,
}

5. Scope Guards — Deterministic Cleanup on Every Path

Zig's errdefer in Rust. Full recipes → zero-cost-safety.md §5.

rust
use scopeguard::guard;

fn deploy(artifact: &Path) -> Result<(), DeployError> {
    let backup = snapshot_current()?;
    // errdefer: restore on failure
    let rollback = guard(backup, |b| { let _ = restore(&b); });

    upload(artifact)?;
    health_check()?;

    // Success: defuse the guard
    scopeguard::ScopeGuard::into_inner(rollback);
    Ok(())
}

6. Bit-Level Layout — zerocopy for Wire Formats

Never hand-write transmute or pointer casts for parsing binary data. Full recipes → zero-cost-safety.md §4.

rust
use zerocopy::{FromBytes, IntoBytes, KnownLayout, Immutable};

#[derive(FromBytes, IntoBytes, KnownLayout, Immutable)]
#[repr(C)]
struct PacketHeader {
    magic: [u8; 4],
    version: u8,
    flags: u8,
    length: [u8; 2], // use byte array for packed fields, decode via from_le_bytes
}

7. Exhaustive Match — No Wildcard on Enums You Control

rust
// WRONG — silently ignores new variants
match status {
    Status::Ok => handle_ok(),
    _ => handle_error(),
}

// RIGHT — compiler forces update when variants change
match status {
    Status::Ok => handle_ok(),
    Status::NotFound => handle_not_found(),
    Status::Timeout => handle_timeout(),
}

For #[non_exhaustive] enums from external crates, the wildcard _ is required — but add a tracing::warn! in the catch-all so you notice when new variants appear.

8. Type-State Over Runtime Checks

Never if self.state == State::Validated. Encode states as distinct types so the compiler refuses invalid transitions. Full patterns → type-state.md.

rust
struct Order<S: OrderState> { data: OrderData, _state: PhantomData<S> }
struct Draft;
struct Validated;
struct Paid;

impl Order<Draft> {
    fn validate(self) -> Result<Order<Validated>, ValidationError> { ... }
}
impl Order<Validated> {
    fn pay(self, payment: Payment) -> Result<Order<Paid>, PaymentError> { ... }
}
// Order<Draft> has no .pay() method. Compiler enforces the workflow.

Standard Library Defaults

Full decision tree with rationale and code snippets → libraries.md.

CategoryCrateWhy
Async runtimetokioEcosystem standard. Patterns → async-tokio.md
HTTP serveraxum + towerType-safe extractors, tower middleware. Stack → axum-stack.md
CLIclap derive + color-eyreTyped args, beautiful errors. Stack → clap-stack.md
Serializationserde + serde_jsonNon-negotiable for any boundary type
Error (library)thiserrorDerive Error with zero boilerplate
Error (binary)anyhow / color-eyreContext-rich ad-hoc errors
Databasesqlx (compile-time checked)No runtime SQL surprises
Arena allocbumpalo / typed-arenaExplicit allocation scope. Patterns → zero-cost-safety.md §1
Zero-copy parsezerocopySafe binary parsing, no transmute. Patterns → zero-cost-safety.md §4
Scope guardscopeguarderrdefer/defer. Patterns → zero-cost-safety.md §5
Stack collectionssmallvec / arrayvec / tinyvecStack-first, heap-spillover. Patterns → zero-cost-safety.md §3
Bitfieldbitfield / modular-bitfieldBit-packed flags. Patterns → zero-cost-safety.md §4
Testingproptest + instaProperty + snapshot tests. Patterns → proptest-insta.md
Concurrencytokio::sync / parking_lotChannel-first, lock-second. Patterns → concurrency.md

Cargo Strict Configuration

Every new project gets the strict lint config from cargo-strict.md. The non-negotiable CI gate:

bash
cargo fmt --all -- --check && \
cargo clippy --all-targets --all-features -- -D warnings && \
cargo nextest run && \
cargo +nightly miri nextest run  # when unsafe is involved

Code Review Checklist (Post-Write, Every PR)

Run through this list after writing any Rust code. Every item links to its recipe.

#CheckFix Reference
1Every function signature prefers &[T]/&str/Cow over owned typeszero-cost-safety.md §3
2Hot-path allocations use arena (bumpalo) not scattered Box/Veczero-cost-safety.md §1
3Const-eligible functions are const fnzero-cost-safety.md §2
4Lookup tables / config constants computed at compile timezero-cost-safety.md §2
5Binary format parsing uses zerocopy, not transmutezero-cost-safety.md §4
6Cleanup logic uses scopeguard or Drop, never manual if err cleanupzero-cost-safety.md §5
7Distinct semantic units are newtypes, not primitive aliasestype-state.md
8State machines use type-state, not runtime if state ==type-state.md
9No unwrap()/expect() outside #[cfg(test)]libraries.md
10Every unsafe has SAFETY comment + miri testunsafe-discipline.md, ../rust-ub/
11Match on owned enums is exhaustive (no _ =>)This file §7
12Clippy pedantic passes with zero warningscargo-strict.md
13Property tests exist for any function with a nontrivial domainproptest-insta.md
14Concurrency uses channels first, locks second, atomics lastconcurrency.md
15Async code uses JoinSet for structured concurrencyasync-tokio.md

Default Cargo.toml Dependencies — Zero-Cost Safety Stack

Every new project starts with these alongside the standard deps from cargo-strict.md:

toml
# Zero-cost safety stack
bumpalo = { version = "3", features = ["collections"] }
scopeguard = "1"
smallvec = { version = "1", features = ["union", "const_generics"] }
zerocopy = { version = "0.8", features = ["derive"] }

# Add when needed:
# typed-arena = "2"           # homogeneous arena
# arrayvec = "0.7"            # fixed-capacity stack vec
# tinyvec = { version = "1", features = ["alloc"] }
# bitfield = "0.17"           # bit-packed flags
# modular-bitfield = "0.11"   # richer bitfield API
# bytemuck = { version = "1", features = ["derive"] }

Reference Index

FileWhen to Load
zero-cost-safety.mdArena, allocator, const fn, comptime, zero-alloc, bitfield, repr, scopeguard, errdefer, Zig-like patterns
type-state.mdNewtype wrappers, type-state machines, branded IDs, phantom types
unsafe-discipline.mdAny unsafe block — SAFETY comments, safe wrappers, miri proof
libraries.mdLibrary selection, crate decision tree, dependency audit
cargo-strict.mdProject bootstrap, lint config, CI gate commands
async-tokio.mdAsync runtime, spawning, cancellation, JoinSet, select!
axum-stack.mdHTTP services — axum + sqlx + tower + tracing
clap-stack.mdCLI tools — clap derive + color-eyre + indicatif
concurrency.mdLocks, atomics, channels, loom model checker
proptest-insta.mdProperty tests, snapshot tests, round-trip invariants
one-liners.mdrust-script one-liners, disposable scripts, inline deps
../rust-ub/README.mdUB hunting — miri escalation, sanitizers, fuzzing
../rust-ub/ub-taxonomy.md14-category UB taxonomy with detection status
../rust-ub/miri-sanitizers-loom.mdMiri flags, ASAN/TSAN/MSAN, loom, cargo-fuzz

The Shape of Every Function

rust
/// One-line doc explaining WHAT, not HOW.
///
/// # Errors
/// Returns `FooError::Bar` when the input is invalid.
const fn frobnicate<'a>(
    arena: &'a Bump,        // explicit allocator when arena is in play
    input: &[u8],           // borrow, not owned
    output: &mut [u8],      // caller-provided buffer
) -> Result<&'a Frob, FrobError> {
    // ...
}

Why this shape: the caller sees every cost. Allocation scope is the arena's lifetime. Input is borrowed. Output buffer is caller-owned. Error is typed. The compiler enforces all of it.


Activation

This skill activates whenever you are writing or modifying any .rs file or Cargo.toml. One-off scripts get the strict treatment too — rust-script + the same lints, the same gates. Details → one-liners.md.

The promise: production hygiene with throwaway ergonomics. Explicit allocation, compile-time proof, zero hidden cost, and agent-proof safety at any volume.