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
ConceptWhat it means
OwnerThe variable that holds a value. Only one owner at a time.
MoveAssigning 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.
CopyStack types (i32, u64, bool, char) copy automatically on assignment. Free and instant.
ScopeThe block between { and } where a variable is valid.
DropWhen the owner goes out of scope, Rust frees the memory automatically. No manual cleanup.
HeapWhere String and Vec live. Data can grow. Must be moved or cloned.
StackWhere i32, bool, char live. Fixed size. Copied automatically.
TermWhat it meansWhen you use it
OwnerThe 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
MoveOwnership 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 traitStack-only types copy automatically on assignment. No move, no clone needed.i32, u64, bool, char and other fixed-size stack types
ScopeThe block between { and } where a variable is valid.Every time you open a code block — functions, loops, if blocks, inner blocks
DropRust frees a value's memory automatically when its owner goes out of scope.Every time a scope ends. No manual cleanup needed.
HeapWhere String and Vec live. Data can grow. Must be moved or cloned.Any type whose size is not known at compile time
StackWhere i32, bool, char live. Fixed size. Copied automatically.Primitive types and other Copy types