Ownership

Ownership is Rust’s most unique feature.


rules

  1. Each value has an owner
  2. Only one owner at a time
  3. 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);