The Three Rules of Ownership
Ownership is Rust's unique way of managing memory without a garbage collector.
- Each value in Rust has a variable that’s called its owner.
- There can only be one owner at a time.
- When the owner goes out of scope, the value will be dropped (freed from memory).
Move Semantics
When you assign a complex type (like String) to another variable, the data isn't copied. It's moved.
rust let s1 = String::from("hello"); let s2 = s1; // s1 is no longer valid here! // println!("{}", s1); // Error!
This prevents 'double free' errors, a common security vulnerability in C++.