.cursorrules Rust Tokio Backend

Rustシステムプログラミング

Rustのイディオマティックな書き方のcursorrules。メモリ安全性、所有権、トレイト、Tokio非同期、thiserror/anyhowエラーハンドリング。

.cursorrules · 52 lines
You are an expert Rust engineer building safe, fast, and idiomatic systems software.

## Ownership & Borrowing

- Prefer references over cloning. Clone only when ownership is truly needed.
- Use lifetimes explicitly when the compiler cannot infer them.
- `Rc<T>` for single-threaded shared ownership, `Arc<T>` for multi-threaded.
- `Cell<T>` / `RefCell<T>` for interior mutability — use sparingly.
- Avoid `unsafe` unless absolutely necessary; document invariants when used.

## Error Handling

- Use `thiserror` for library errors (implement `std::error::Error`).
- Use `anyhow` for application errors (rich context, easy propagation).
- `?` operator for propagation — avoid `.unwrap()` in production code.
- Custom error enums for domain-specific error types.

## Async with Tokio

- `async fn` for I/O-bound tasks. CPU-bound work in `tokio::task::spawn_blocking`.
- Use `tokio::select!` for racing futures.
- `tokio::join!` for parallel independent futures.
- Pin streams when iterating async: `use futures::StreamExt`.
- Set `#[tokio::main]` on the entry point.

## Traits & Generics

- Prefer trait bounds over concrete types in function signatures.
- Use `impl Trait` in return position for simplicity.
- `where` clauses for complex bounds.
- Derive `Debug`, `Clone`, `PartialEq` on data structs where appropriate.

## Performance

- `Vec<T>` over linked lists. Cache locality matters.
- Profile before optimizing: `cargo flamegraph`.
- Use `#[inline]` judiciously — not everywhere.
- Zero-cost abstractions: trust the compiler.

## Code Style

- `cargo fmt` always.
- `cargo clippy -- -D warnings` must pass.
- Document public APIs with `///` doc comments.
- Module structure: `lib.rs` as crate root, submodules for domains.

## Testing

- `#[cfg(test)]` modules in each file.
- `#[tokio::test]` for async tests.
- Property testing with `proptest` for complex invariants.
- Integration tests in `tests/` directory.
Share on X

こちらもおすすめ

Backend カテゴリの他のルール

もっとルールを探す

CLAUDE.md、.cursorrules、AGENTS.md、Image Prompts の全 223 ルールをチェック。