Home › All rules › Community › Rust General Cursor Rules

Rust General Cursor Rules

General Rust rules for safe, idiomatic application and library development

CursorRustCommunity (CC0)

How to use

Save this as .cursor/rules/rust-general.mdc in your project, commit it, and Cursor will load it. Review it first: community files can be opinionated or out of date.

Note: Review and test before use. Provided as is, without warranty. See Terms.

.cursor/rules/rust-general.mdcDownload  
---
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.

More: Rust rules · all community rules

Related rules

Rust Actix Web rules

Cursor rules for Actix Web 4 services in Rust: app state, extractors, ResponseError, middleware, and integration tests.

CursorRust

Rust async (Tokio) .claude/rules file

A path-scoped Claude Code rules file for async Rust on Tokio: no blocking, cancellation safety, locks, and tasks.

Claude Code (CLAUDE.md)Rust

Rust Axum web API rules

Cursor rules for Rust web services with Axum 0.8, Tokio, sqlx, typed errors, and tracing.

CursorRust

Rust Cursor Rules

Rust best practices for Solana smart contract development using Anchor framework and Solana SDK

CursorRustCommunity (CC0)

Rust Mcp Server Copilot Instructions

Best practices for building Model Context Protocol servers in Rust using the official rmcp SDK with async/await patterns

GitHub CopilotRustCommunity (MIT)

Rust Copilot Instructions

Rust programming language coding conventions and best practices

GitHub CopilotRustCommunity (MIT)

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.