Best practices for Rust Memory Safety Guidelines
.cursorrules in your project root# Rust Memory Safety Guidelines ===================================== ## Core Principles ------------------ * **Ownership System**: Rust's ownership system is based on the concept of ownership and borrowing. Each value in Rust has an owner that is responsible for deallocating the value when it is no longer needed. * **Borrow Checker**: The borrow checker is a key component of Rust's ownership system. It ensures that references to values are valid and that there are no dangling pointers. * **Null Safety**: Rust does not have null pointers. Instead, it uses `Option` and `Result` types to handle the possibility of a value being absent. ## Code Style Guidelines ----------------------- ### Naming * **Variable Names**: Use descriptive and concise variable names. Avoid using single letter variable names except for loop counters. * **Function Names**: Use descriptive and concise function names. Use verbs or verb phrases to describe what the function does. * **Type Names**: Use descriptive and concise type names. Use nouns or noun phrases to describe what the type represents. ### Typing * **Type Annotations**: Use type annotations to specify the type of a variable, function parameter, or return value. * **Type Inference**: Use type inference to let the compiler infer the type of a variable or expression. * **Type Aliases**: Use type aliases to create a new name for an existing type. ## Best Practices ------------------ ### Memory Management * **Use Smart Pointers**: Use smart pointers like `Box`, `Rc`, and `Arc` to manage memory. * **Avoid Raw Pointers**: Avoid using raw pointers like `*const T` and `*mut T` unless absolutely necessary. * **Use Drop**: Implement the `Drop` trait to release resources when a value is dropped. ### Error Handling * **Use Result**: Use the `Result` type to handle errors. * **Use Option**: Use the `Option` type to handle the possibility of a value being absent. * **Avoid Panic**: Avoid using `panic!` unless absolutely necessary. Instead, use `Result` or `Option` to handle errors. ### Code Organization * **Use Modules**: Use modules to organize code into logical units. * **Use Functions**: Use functions to break down code into smaller, reusable pieces. * **Use Traits**: Use traits to define interfaces and behaviors. ## Common Pitfalls to Avoid --------------------------- ### Dangling Pointers * **Avoid Returning References to Local Variables**: Avoid returning references to local variables because they will be dropped when the function returns. * **Avoid Returning References to Temporary Values**: Avoid returning references to temporary values because they will be dropped at the end of the statement. ### Null Pointer Dereferences * **Avoid Using Null Pointers**: Avoid using null pointers because they can cause null pointer dereferences. * **Use Option Instead of Null**: Use `Option` instead of null to handle the possibility of a value being absent. ### Data Races * **Use Mutexes**: Use mutexes to protect shared data from concurrent access. * **Use Atomic Types**: Use atomic types to update shared data safely. ### Memory Leaks * **Use Drop**: Implement the `Drop` trait to release resources when a value is dropped. * **Avoid Circular References**: Avoid creating circular references because they can cause memory leaks.
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
coding
AI coding rules customize how Cursor generates and refactors code for your project. Follow these steps to install Rust Memory Safety Guidelines.
.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.