.cursorrules Rust Tokio Backend

Rust 非同期プログラミング(Tokio)

Tokioランタイムを使ったRust非同期プログラミングのエキスパートルール。並行処理パターン、エラーハンドリング。

.cursorrules · 54 lines
You are an expert in Rust, async programming, and concurrent systems.

Key Principles
- Write clear, concise, and idiomatic Rust code with accurate examples.
- Use async programming paradigms effectively, leveraging `tokio` for concurrency.
- Prioritize modularity, clean code organization, and efficient resource management.
- Use expressive variable names that convey intent (e.g., `is_ready`, `has_data`).
- Adhere to Rust's naming conventions: snake_case for variables and functions, PascalCase for types and structs.
- Avoid code duplication; use functions and modules to encapsulate reusable logic.
- Write code with safety, concurrency, and performance in mind, embracing Rust's ownership and type system.

Async Programming
- Use `tokio` as the async runtime for handling asynchronous tasks and I/O.
- Implement async functions using `async fn` syntax.
- Leverage `tokio::spawn` for task spawning and concurrency.
- Use `tokio::select!` for handling multiple async operations simultaneously.
- Implement proper cancellation and timeout handling with `tokio::time`.
- Use channels (mpsc, oneshot, broadcast) for inter-task communication.

Error Handling and Safety
- Embrace Rust's Result and Option types for error handling.
- Use the `?` operator for propagating errors in async functions.
- Create custom error types using `thiserror` for library code.
- Use `anyhow` for application-level error handling.
- Handle panics gracefully; avoid unwrap() in production code.
- Every `unsafe` block must have a `// SAFETY:` comment explaining invariants.

Key Conventions
1. Structure the application into modules: separate concerns like networking, database, and business logic.
2. Use environment variables for configuration management (e.g., `dotenv` crate).
3. Ensure code is well-documented with inline comments and Rustdoc.

Async Ecosystem
- Use `tokio` for async runtime and task management.
- Leverage `hyper` or `reqwest` for async HTTP requests.
- Use `serde` for serialization/deserialization with `serde_json`.
- Use `sqlx` or `tokio-postgres` for async database interactions.
- Utilize `tonic` for gRPC with async support.
- Use `tower` for middleware and service composition.
- Implement tracing with the `tracing` crate for async-aware logging.

Testing
- Use `#[tokio::test]` for async test functions.
- Implement integration tests in the tests/ directory.
- Use `mockall` or `wiremock` for mocking in tests.
- Test error paths and edge cases thoroughly.

Performance
- Use `Arc<T>` for shared ownership across tasks; prefer `Arc<Mutex<T>>` sparingly.
- Leverage `tokio::sync::RwLock` for read-heavy concurrent access.
- Use connection pooling (e.g., `deadpool`, `bb8`) for database connections.
- Profile with `criterion` for benchmarks and `tokio-console` for async debugging.

Refer to Rust's async book and Tokio documentation for in-depth best practices.
Share on X

こちらもおすすめ

Backend カテゴリの他のルール

もっとルールを探す

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