Cursor rules for Rust development with focus on memory safety, ownership, and performance.
.cursorrules in your project rootYou are an expert Rust systems programmer.
## Ownership & Borrowing
```rust
// ✅ Prefer borrowing over ownership transfer
fn process_data(data: &[u8]) -> Result<(), Error> {
// Process without taking ownership
}
// ✅ Use mutable references when needed
fn modify_data(data: &mut Vec<u8>) {
data.push(0);
}
// ✅ Use Cow for flexibility
use std::borrow::Cow;
fn process_string(input: Cow<str>) -> Cow<str> {
if input.contains("bad") {
Cow::Owned(input.replace("bad", "good"))
} else {
input
}
}
```
## Error Handling
```rust
use thiserror::Error;
use anyhow::{Context, Result};
#[derive(Error, Debug)]
pub enum AppError {
#[error("Database error: {0}")]
Database(#[from] sqlx::Error),
#[error("Validation error: {0}")]
Validation(String),
#[error("Not found: {0}")]
NotFound(String),
}
// Use anyhow for application code
fn load_config() -> Result<Config> {
let content = std::fs::read_to_string("config.toml")
.context("Failed to read config file")?;
let config: Config = toml::from_str(&content)
.context("Failed to parse config")?;
Ok(config)
}
// Use thiserror for libraries
pub fn get_user(id: &str) -> Result<User, AppError> {
users.get(id)
.ok_or_else(|| AppError::NotFound(format!("User {id} not found")))
}
```
## Async Rust
```rust
use tokio::sync::{mpsc, Mutex};
use std::sync::Arc;
// ✅ Use tokio for async runtime
#[tokio::main]
async fn main() -> Result<()> {
let app = App::new().await?;
app.run().await
}
// ✅ Share state safely
struct AppState {
db: Pool<Postgres>,
cache: Arc<Mutex<HashMap<String, Value>>>,
}
// ✅ Use channels for communication
async fn spawn_worker(mut rx: mpsc::Receiver<Task>) {
while let Some(task) = rx.recv().await {
process_task(task).await;
}
}
```
## Performance
```rust
// ✅ Use iterators
let sum: i32 = numbers.iter().filter(|&&x| x > 0).sum();
// ✅ Avoid unnecessary allocations
fn process(input: &str) -> &str {
input.trim() // Returns slice, no allocation
}
// ✅ Use capacity hints
let mut vec = Vec::with_capacity(1000);
// ✅ Profile before optimizing
// Use cargo flamegraph, criterion for benchmarks
```
## Project Structure
```
src/
├── lib.rs # Library root
├── main.rs # Binary entry
├── config.rs # Configuration
├── error.rs # Error types
├── db/
│ ├── mod.rs
│ └── models.rs
├── api/
│ ├── mod.rs
│ └── handlers.rs
└── services/
└── mod.rs
```Comprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.
Cursor
systems
AI coding rules customize how Cursor generates and refactors code for your project. Follow these steps to install Rust Systems Programming.
.cursor/rules, for Windsurf use .windsurfrulesComprehensive Cursor rules for Next.js 14+ with App Router, including routing, layouts, and API patterns.
Cursor rules for TypeScript with strict type checking, advanced patterns, and best practices.
Cursor rules for Tailwind CSS development with responsive design, custom components, and dark mode.