cd ../labs
Rustintermediateabout 35 minutes
Rust: ownership and borrowing
The idea that makes Rust different: who owns a value, who may borrow it, and why the compiler refuses what it refuses.
By the end you can
- Explain what owning a value means
- Tell a move from a copy
- Read borrow checker errors instead of fighting them
- Know when to use &, &mut and clone
0 of 2 done
step 1 of 2
Moves and copies
Every value in Rust has exactly one owner. When the owner goes out of scope, the value is freed. No garbage collector, no manual free.
Assigning a heap value to another name moves it: the old name stops being usable. Small stack values like integers are copied instead.
This is what buys Rust memory safety without a runtime, and it is the part that feels strange for a week and then feels obvious.
question 1 of 3
Why does this not compile?
let a = String::from("kali"); let b = a; println!("{}", a);question 2 of 3
Why does the same pattern compile for integers?
let x = 5; let y = x; println!("{}", x);question 3 of 3
What happens to the String after this function call?
fn consume(s: String) { } let name = String::from("drews"); consume(name); println!("{}", name);