Rust Tutorial: Ownership
Move semantics, the Copy trait, clone, and how Rust manages memory without a garbage collector.
The feature that makes Rust unique
Every value has exactly one owner. When the owner goes out of scope, the value is freed automatically — no GC, no malloc/free.
What you'll learn in this Rust ownership tutorial
This interactive Rust tutorial has 8 hands-on exercises. Estimated time: 22 minutes.
- Values drop at end of scope — When a variable goes out of scope, Rust drops its value and frees memory.
- Move semantics — Assigning a `String` moves ownership. The original variable becomes invalid.
- Clone — `.clone()` makes a deep copy. Both variables own independent copies.
- Copy types — Simple stack types like integers implement `Copy`. Assigning them copies the value — no move.
- Passing to functions moves — Passing a non-Copy value to a function moves it.
- Return gives back ownership — A function can return a value to transfer ownership back to the caller.
- Stack vs heap — Stack values (integers, booleans, arrays of fixed size) implement Copy.
- Ownership summary — Three rules:
Rust Ownership concepts covered
- The feature that makes Rust unique