Rust memory-safety guidelines: ownership and borrowing first, smart pointers only when needed, Weak for cycles, Send/Sync for concurrency, and leak awareness.
.cursor/rules/rust-memory-safety-guidelines.mdc. Downloaded files need to be placed in that repository path.--- description: "Rust memory-safety guidelines: ownership and borrowing first, smart pointers only when needed, Weak for cycles, Send/Sync for concurrency, and leak awareness." globs: "**/*" alwaysApply: false --- Apply these guidelines when writing or reviewing Rust code for memory safety. ## Core model - Every value has one owner and is dropped when the owner goes out of scope. Borrow (`&T`, `&mut T`) when a function only needs to read or modify a value; take ownership only when it stores or consumes it. - The borrow checker guarantees that references in safe Rust never dangle and that a value is not mutated while shared. Restructure code (shorter borrows, splitting structs, indices instead of references) before reaching for `clone()` or `unsafe` to satisfy it. - Safe Rust has no null references. Model absence with `Option<T>` and failure with `Result<T, E>`. Raw pointers can be null; check them at FFI boundaries before converting to references. ## Heap allocation and shared ownership - Prefer plain owned values and borrows. Use `Box<T>` for recursive types, large values or trait objects; `Rc<T>` (single thread) or `Arc<T>` (across threads) only when a value truly has several owners. - `Rc`/`Arc` cycles are never freed. Use `Weak<T>` for back-references (child → parent, observer → subject). - Use `RefCell`/`Mutex` interior mutability only where a shared value must change; it moves borrow checks to run time. ## Resources and Drop - Most types need no `Drop` impl; their fields are dropped automatically. Implement `Drop` only for types that own an external resource (file descriptor, FFI handle, lock) and must release it. - Leaking memory is not undefined behaviour in Rust (`mem::forget`, `Box::leak`, `Rc` cycles), so the compiler will not stop it. Check long-lived caches and reference cycles. ## Concurrency - Data races are prevented at compile time through the `Send` and `Sync` traits. To share mutable data between threads, use `Arc<Mutex<T>>`/`Arc<RwLock<T>>`, atomics for single counters or flags, or message passing with channels. - Keep lock scopes short and avoid acquiring locks in different orders (deadlocks are still possible in safe Rust). ## Unsafe code - Avoid raw pointers and `unsafe` unless needed for FFI or a measured performance gain. Wrap any `unsafe` in a small safe API and document why it is sound with a `// SAFETY:` comment. ## Common pitfalls - Returning a reference to a local or temporary: return an owned value instead (the compiler rejects the reference). - Cloning to silence the borrow checker in hot paths: check whether a borrow or `Cow` would do. - Holding a `RefCell` borrow or a `Mutex` guard while calling code that borrows/locks the same value: panics or deadlocks at run time.
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.
Cursor
coding
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
A tree of UI nodes stores Rc<RefCell<Node>> for both children and parent links, and memory grows each time a subtree is replaced.
Using the Rust Memory Safety Guidelines rule, review src/tree.rs for ownership and leak issues.
The review identifies the Rc parent link as a reference cycle and changes it to Weak<RefCell<Node>>, shows upgrade() at use sites, removes an unnecessary Drop impl, shortens RefCell borrows that span callbacks, and notes that leaks are not caught by the compiler.
Cursor applies project rules stored as .mdc files in .cursor/rules. Each rule's frontmatter (description, globs, alwaysApply) decides whether it is always included, attached to matching files, chosen by the agent from its description, or only used when you @-mention it. Cursor also reads AGENTS.md in the project root and subdirectories for plain-Markdown instructions.
.cursor/rules/rust-memory-safety-guidelines.mdcStarting point, not a tested configuration for your repository. Attached when files matching the globs are in context (alwaysApply: false with globs). Review the instructions, adapt the file paths and run your project's existing checks.
--- description: "Rust Memory Safety Guidelines project conventions" globs: "**/*" alwaysApply: false --- Apply these guidelines when writing or reviewing Rust code for memory safety. ## Core model - Every value has one owner and is dropped when the owner goes out of scope. Borrow (`&T`, `&mut T`) when a function only needs to read or modify a value; take ownership only when it stores or consumes it. - The borrow checker guarantees that references in safe Rust never dangle and that a value is not mutated while shared. Restructure code (shorter borrows, splitting structs, indices instead of references) before reaching for `clone()` or `unsafe` to satisfy it. - Safe Rust has no null references. Model absence with `Option<T>` and failure with `Result<T, E>`. Raw pointers can be null; check them at FFI boundaries before converting to references. ## Heap allocation and shared ownership - Prefer plain owned values and borrows. Use `Box<T>` for recursive types, large values or trait objects; `Rc<T>` (single thread) or `Arc<T>` (across threads) only when a value truly has several owners. - `Rc`/`Arc` cycles are never freed. Use `Weak<T>` for back-references (child → parent, observer → subject). - Use `RefCell`/`Mutex` interior mutability only where a shared value must change; it moves borrow checks to run time. ## Resources and Drop - Most types need no `Drop` impl; their fields are dropped automatically. Implement `Drop` only for types that own an external resource (file descriptor, FFI handle, lock) and must release it. - Leaking memory is not undefined behaviour in Rust (`mem::forget`, `Box::leak`, `Rc` cycles), so the compiler will not stop it. Check long-lived caches and reference cycles. ## Concurrency - Data races are prevented at compile time through the `Send` and `Sync` traits. To share mutable data between threads, use `Arc<Mutex<T>>`/`Arc<RwLock<T>>`, atomics for single counters or flags, or message passing with channels. - Keep lock scopes short and avoid acquiring locks in different orders (deadlocks are still possible in safe Rust). ## Unsafe code - Avoid raw pointers and `unsafe` unless needed for FFI or a measured performance gain. Wrap any `unsafe` in a small safe API and document why it is sound with a `// SAFETY:` comment. ## Common pitfalls - Returning a reference to a local or temporary: return an owned value instead (the compiler rejects the reference). - Cloning to silence the borrow checker in hot paths: check whether a borrow or `Cow` would do. - Holding a `RefCell` borrow or a `Mutex` guard while calling code that borrows/locks the same value: panics or deadlocks at run time.
Ask for a small change to one matching file. Check the tool's loaded context or instruction view, inspect the diff for the requested convention, and run the relevant build or test. A well-formatted file does not guarantee the model follows every instruction.
Cursor's docs recommend keeping individual rules under 500 lines and splitting large rules into composable ones. Cursor's documentation states that it supports AGENTS.md in the project root and in subdirectories. Use AGENTS.md for plain-Markdown instructions you also want other tools to read, and .mdc rules when you need Cursor's scoping options (globs, description-based selection, always-apply).
Documentation checked 2026-11-05: Cursor docs: Rules
Cursor rules for Next.js 14+ App Router projects updated for Next.js 16 with async params, validated Route Handlers, generateMetadata and proxy.ts in place of middleware.
Cursor rules for strict TypeScript: compiler checks beyond strict, interfaces vs type aliases, real type guards, discriminated unions, utility types and branded IDs.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.