Skip to content
Justice Tech School

Ownership

Each value has one owner. Assigning or passing moves ownership. The value is dropped when the owner leaves scope.

Example
fn take(s: String) { println!("took {}", s); }

fn main() {
    let a = String::from("hi");
    let b = a;
    println!("{}", b);
    take(b);
    println!("moved");
}

Result

hi
took hi
moved

Exercise

Copy for small types.

let x = 5; let y = ;

Quiz Test

What happens on let b = a for a String?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.