Ownership
Ownership is Rust’s most unique feature.
rules
- Each value has an owner
- Only one owner at a time
- When owner goes out of scope, value is dropped
copy vs move
| Type | Behavior |
|---|---|
i32, bool, f64 |
Copied — stack types |
String, Vec |
Moved — heap types |
example
fn main() {
let s1 = String::from("hello");
let s2 = s1;
println!("{}", s2);
}
clone
let s1 = String::from("hello");
let s2 = s1.clone();
println!("{} {}", s1, s2);