Ownership (Rust)
Ownership is Rust's core memory-management rule: every value has exactly one owner variable, and when that owner goes out of scope the value is automatically freed. No garbage collector, no manual free().
Rust
Ownership is the idea that makes Rust memory-safe without a garbage collector. The three rules are: each value has one owner, there can only be one owner at a time, and when the owner goes out of scope the value is dropped (its memory freed). Because the rules are checked at compile time, there is zero runtime cost.
When you assign a heap value (like a String or Vec) to another variable or pass it to a function, ownership moves — the original variable is no longer usable. This is what prevents double-frees and use-after-free bugs that plague C and C++.
If you want to use a value without taking ownership, you borrow it with a reference (&). If you need an independent copy, you call .clone(). Small stack-only types that implement Copy (like integers) are duplicated automatically instead of moved.
Examples
Rust
fn main() {
let s1 = String::from("hi");
let s2 = s1; // ownership MOVES to s2
// println!("{}", s1); // compile error: s1 was moved
println!("{}", s2);
}Output:
hi