rust-general — Cursor rule: what it does, how to install
This rule guides safe, idiomatic Rust work. It fits projects where .rs, Cargo.toml, and Cargo.lock need consistent coding guidance.
.cursor/rules/rust-general.mdcWhat rust-general does
rust-general covers Rust project layout, ownership, type choices, error handling, async work, and tests. It favors focused crates, small documented public APIs, borrowing when ownership is unnecessary, and explicit domain states with enums and structs. Libraries can use thiserror or the project’s error style, while applications can use anyhow with context at I/O, network, database, and parsing boundaries.
The rule also addresses Send and Sync, cancellation, blocking work in async applications, and test selection. Before delivery, it calls for cargo fmt and cargo clippy; pure logic and public behavior get different test scopes. Its file patterns cover **/*.rs, Cargo.toml, and Cargo.lock, while alwaysApply is false.
Code that keeps a blocking lock across .await conflicts with this guidance. The same applies to undocumented unsafe invariants and discarded errors hidden behind _.
What it helps with
- organize Rust crates by domain responsibility
- model absent and fallible values with
Option<T>andResult<T, E> - attach context at I/O, network, database, or parsing boundaries
- inspect async code for blocking work across
.await - run
cargo fmtandcargo clippybefore delivery
Example prompts for the agent
- Review this Rust module against the rule for ownership, errors, and project structure.
- Find unnecessary clones in
src/lib.rsand suggest borrowing where the API allows it. - Check the async code for blocking locks across
.awaitand hidden cancellation.
How to install rust-general
Manually: put the files in place
.cursor/rules/rust-general.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.claude/rules/rust-general.mdThe paths frontmatter field limits the files: move globs from the source into paths
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.github/instructions/rust-general.instructions.mdThe applyTo field sets which files it applies to: move globs from the source into applyTo
Manually: put the files in place
.devin/rules/rust-general.mdManually: put the files in place
.clinerules/rust-general.mdStep by step, with commands for each platform: Cursor rules: .mdc files, four modes and what replaced .cursorrules
Questions about rust-general
How do I install rust-general in Cursor?
Place the rule file at .cursor/rules/rust-general.mdc. Cursor can then apply it to files matching the configured patterns.
What does rust-general do?
It sets guidance for Rust crate structure, ownership, types, errors, concurrency, async code, and testing. It also asks for cargo fmt and cargo clippy before delivery.
Which files does rust-general cover?
Its patterns target **/*.rs, Cargo.toml, and Cargo.lock. The rule is not always active because alwaysApply is false.
Source text
rust-general.mdc · CC0-1.0
--- description: General Rust rules for safe, idiomatic application and library development globs: ["**/*.rs", "Cargo.toml", "Cargo.lock"] alwaysApply: false --- # Rust General Rules ## Project Structure - Keep crates focused and name modules by domain responsibility. - Put reusable library code in `src/lib.rs` and binary entry points in `src/main.rs` or `src/bin/`. - Keep public APIs small and documented. - Use feature flags deliberately and document non-default features. - Commit `Cargo.lock` for applications; follow the project convention for libraries. ## Ownership and Types - Prefer borrowing over cloning when ownership is not needed. - Use owned values at API boundaries when the callee must store data. - Model domain states with enums and structs instead of strings or booleans. - Use `Option<T>` for absence and `Result<T, E>` for fallible operations. - Avoid `unwrap()` and `expect()` outside tests, examples, and process-startup invariants. ## Error Handling - Use `thiserror` or project-standard custom errors for libraries. - Use `anyhow` or project-standard context-rich errors for applications. - Add context when crossing IO, network, database, or parsing boundaries. - Do not discard errors with `_` unless explicitly documented. ## Concurrency and Async - Use `Send` and `Sync` boundaries intentionally. - Prefer message passing or owned task inputs for async work. - Do not hold blocking locks across `.await`. - Use `tokio::task::spawn_blocking` or equivalent for blocking CPU or IO in async applications. - Propagate cancellation through futures rather than hiding it in detached tasks. ## Testing and Quality - Run `cargo fmt` and `cargo clippy` before delivery. - Add unit tests for pure logic and integration tests for public behavior. - Use property tests for parsers, serializers, and state machines when useful. - Use benchmarks only after identifying a real performance question. ## Common Mistakes - Do not fight the borrow checker by adding unnecessary `Arc<Mutex<_>>`. - Do not expose internal module structure through public APIs by accident. - Do not allocate in hot loops without measuring. - Do not use unsafe code unless the invariant is documented and tested.
© PatrickJS · CC0-1.0 · PatrickJS/awesome-cursorrules