rust general
via PatrickJS/awesome-cursorrules
General Rust rules for safe, idiomatic application and library development
What is rust general?
Covers project structure, ownership, error handling, concurrency, and testing practices for Rust applications and libraries. Use this rule to enforce consistent patterns across Rust codebases, from API design to async safety and error propagation.
- Enforce focused crate design with clear separation of library and binary code
- Guide ownership and type modeling using Option, Result, and enums instead of primitives
- Establish error handling patterns with thiserror for libraries and anyhow for applications
- Ensure async safety by preventing blocking locks across await points and managing task cancellation
- Require code quality checks via cargo fmt and clippy before delivery
- Discourage common pitfalls like unnecessary Arc<Mutex<_>>, unsafe code without documentation, and hot-loop allocations
Applies to
File patterns this rule matches.
Rule definition (reference)
Source of truth, from the repository.
Rust General Rules
Project Structure
- Keep crates focused and name modules by domain responsibility.
- Put reusable library code in
src/lib.rsand binary entry points insrc/main.rsorsrc/bin/. - Keep public APIs small and documented.
- Use feature flags deliberately and document non-default features.
- Commit
Cargo.lockfor 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 andResult<T, E>for fallible operations. - Avoid
unwrap()andexpect()outside tests, examples, and process-startup invariants.
Error Handling
- Use
thiserroror project-standard custom errors for libraries. - Use
anyhowor 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
SendandSyncboundaries intentionally. - Prefer message passing or owned task inputs for async work.
- Do not hold blocking locks across
.await. - Use
tokio::task::spawn_blockingor 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 fmtandcargo clippybefore 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.
Related rules
Senior Salesforce Apex development guide with design patterns, testability, and best practices.
Scala and Kafka development practices with clean code, functional patterns, and testing guidelines.

security devsecops ssdls appsec
Secure coding, secret handling, dependency hygiene, and compliance for DevSecOps and SSDLC.
30+ chart types for React with AI-assisted generation, real-time streaming, and geographic visualization.
Best practices for Shopify Liquid theme development with structured project organization, performance optimization, and accessibility.
SQL-callable AI functions and hybrid vector+keyword search for RAG within Snowflake.