Jcorpac AI Skills Library Rs Memory Safety

Rust Memory Safety

tomevault-io Updated

File contents

Rust Memory Safety

Rust's borrow checker is a strict but fair mentor. This skill codifies the patterns for working with it effectively.

The Trinity of Ownership

  1. Ownership: Each value has a single owner.
  2. Borrowing:
    • Unlimited immutable borrows (&T).
    • EXACTLY ONE mutable borrow (&mut T) at a time.
  3. Lifetimes: Ensuring references are never valid longer than the data they point to.

Smart Pointers

  • Box<T>: Heap allocation for data with a known size at compile time.
  • Rc<T> / Arc<T>: Reference counting for shared ownership (Arc is thread-safe).
  • RefCell<T>: Interior mutability (checked at runtime).

Best Practices

  • Prefer borrowing over cloning where possible.
  • Use Struct lifetimes ('a) only when the struct doesn't own its data.
  • Avoid unsafe unless performing FFI or low-level optimizations.

Converted and distributed by TomeVault — claim your Tome and manage your conversions.

tomevault-io/skills-registry/tree/main/jcorpac--ai-skills-library--rs-memory-safety commit c3d51e5749

Frequently asked questions

npx skillmds@latest add tomevault-io/jcorpac-ai-skills-library-rs-memory-safety