Topic
Ownership
Ownership is how Rust manages memory without a garbage collector. Every value has exactly one owner. When the owner goes out of scope, Rust frees the memory automatically. This prevents dangling pointers, double-free errors, and memory leaks without needing a garbage collector.
Quick Reference
| Concept | What it means |
|---|---|
| Owner | The variable that holds a value. Only one owner at a time. |
| Move | Assigning a String to a new variable transfers ownership. The old variable becomes invalid. |
| .clone() | Deep copy — duplicates heap data so both variables stay valid. Costs memory. |
| Copy | Stack types (i32, u64, bool, char) copy automatically on assignment. Free and instant. |
| Scope | The block between { and } where a variable is valid. |
| Drop | When the owner goes out of scope, Rust frees the memory automatically. No manual cleanup. |
| Heap | Where String and Vec live. Data can grow. Must be moved or cloned. |
| Stack | Where i32, bool, char live. Fixed size. Copied automatically. |
| Term | What it means | When you use it |
|---|---|---|
| Owner | The variable responsible for a value. When it goes out of scope, the value is freed. | Every value in Rust has exactly one owner at a time |
| Move | Ownership transfers to a new variable. The original can no longer be used. | Any time you assign a String or Vec to another variable or pass it to a function |
| .clone() | Makes a full independent copy of heap data. Both variables stay valid. Costs memory. | When you need two valid copies of a String or Vec |
| Copy trait | Stack-only types copy automatically on assignment. No move, no clone needed. | i32, u64, bool, char and other fixed-size stack types |
| Scope | The block between { and } where a variable is valid. | Every time you open a code block — functions, loops, if blocks, inner blocks |
| Drop | Rust frees a value's memory automatically when its owner goes out of scope. | Every time a scope ends. No manual cleanup needed. |
| Heap | Where String and Vec live. Data can grow. Must be moved or cloned. | Any type whose size is not known at compile time |
| Stack | Where i32, bool, char live. Fixed size. Copied automatically. | Primitive types and other Copy types |