# Rs Memory Safety

> Deep dive into Rust's core promise: memory safety without a garbage collector via ownership and lifetimes.

- Skill: `jcorpac/rs-memory-safety` (Agent Skill)
- Install (CLI): `npx skillmds@latest add jcorpac/rs-memory-safety`
- Raw SKILL.md: https://api.skillmd.com/api/skills/jcorpac/rs-memory-safety/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: jcorpac (https://skillmd.com/u/jcorpac)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/jcorpac/rs-memory-safety

---


# 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.


