rust

A set of Rust rules for borrowing data, choosing function parameter types, and handling errors with Result-based libraries.

In plain words
What is it for?
Use it when designing Rust function parameters, managing ownership and lifetimes, and choosing error handling for libraries or applications.
Why use it?
It reduces unnecessary copying and prevents production failures caused by unexplained panics or poorly structured errors.

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/cursor-doctor/rust
Clone the repo
git clone --depth 1 https://github.com/nedcodes-ok/cursor-doctor
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 100% copy Near-identical to another mod 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

This is a copy

100% identical to rust — 0 lines differ, which has more behind it and is treated as the original. This page carries a canonical link to it rather than competing with it.

pro-kit/templates/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/cursor-doctor (9 stars, last pushed 5mo 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. It is 100% identical to rust, differing in 0 lines, and is treated as a copy.