rust

A set of coding rules for Rust, a programming language that checks memory ownership and borrowing at compile time. It covers borrowing, error results, iterators, and suitable error libraries.

In plain words
What is it for?
Use it when writing or reviewing Rust functions, borrowed parameters, error types, iterator code, and application or library error handling.
Why use it?
It helps avoid unnecessary copying, unclear ownership, and production crashes caused by unhandled errors or forced unwraps.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/nedcodes-ok/cursorrules-collection/rust
Clone the repo
git clone --depth 1 https://github.com/nedcodes-ok/cursorrules-collection
Per session 1,085 This file is loaded in full into every session.
When invoked 1,085 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.01085 $0.01085
Opus 5 $0.00543 $0.00543
Sonnet 5 $0.00217 $0.00217
Haiku 4.5 $0.00109 $0.00109

Measured yesterday against content hash 0ed760a09588, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

rust scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

Origin

Copies of this mod

1 near-identical copy found in the catalogue:

  • rust — 100% identical, 0 lines differ
rules-mdc/languages/rust.mdc · 53 lines

How it starts

The opening of the file, as written. The whole thing — 53 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Rust Cursor Rules

You are an expert Rust developer. Follow these rules:

Ownership & Borrowing

  • Prefer borrowing (&T, &mut T) over cloning — .clone() is a code smell unless the type is cheap to clone (small Copy types, Arc)
  • Use explicit lifetime annotations only when the compiler can't infer them (multiple references in, one reference out). Don't litter code with 'a when elision handles it
  • Parameters: &str over &String, &[T] over &Vec<T> — accepting the owned type forces callers to allocate unnecessarily. The reference form accepts both owned and borrowed
  • Cow<'_, str> when a function sometimes allocates and sometimes doesn't — avoids cloning in the common path while allowing owned data when needed
  • impl AsRef<str> or Into<String> for parameters that should accept both &str and String ergonomically

Error Handling

  • thiserror for library errors (derive Error with structured variants). anyhow for application code (captures any error with context)
  • Never .unwrap() in production code — it panics with no context. .expect("reason why this should never fail") is acceptable only for genuinely impossible states (verified by prior logic)
  • ? operator for propagation — it's the entire error handling strategy in Rust. Design functions to return Result<T, E> so ? composes naturally
  • Add context with .context("what we were trying to do") (anyhow) or .map_err(|e| MyError::Fetch { source: e }) (thiserror) — raw propagation without context creates opaque error chains
  • panic! only for programmer bugs (violated invariants, unreachable states). Never for user input, network errors, or file operations

Type System

  • Newtype pattern for type safety: struct UserId(u64) prevents passing an OrderId where a UserId is expected — zero runtime cost
  • Enums with data over boolean flags: enum Status { Active, Suspended { reason: String, until: DateTime } } not is_active: bool, suspension_reason: Option<String>
  • Derive Debug, Clone, PartialEq at minimum on data types. Add Serialize, Deserialize (serde) when the type crosses boundaries (API, storage, config)
  • Builder pattern (or Default + struct update) for types with many optional fields — don't make callers construct 10-field structs manually

Read the full file on GitHub · 53 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 53 lines · 1,085 tokens per session scan A 0ed760a09588

Subscribe to this mod's changes

rust is a cursor rule published in the GitHub repository nedcodes-ok/cursorrules-collection (37 stars, last pushed 6mo ago), licensed MIT. It adds 1,085 tokens to every session, about $0.0054 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.