Rust Fundamentals · 15 XP Pro
Moves
Every value in Rust has exactly one owner: the variable that holds it. When the
owner goes out of scope, the value is cleaned up. There is no garbage collector and
no manual free; this one rule does the job.
A String owns heap memory, so assigning it to another variable does not copy the
text. It moves ownership, and the old name becomes unusable:
let a = String::from("rust");
let b = a; // ownership moves from a to b
// println!("{a}"); // error[E0382]: borrow of moved value: `a`
println!("{b}");
This stops two owners from freeing the same memory. The compiler reports the mistake instead of letting it become a crash at run time.
Moves also happen when you pass a value to a function or put it in a tuple. A workable habit: finish using a value (for example, read its length) before you hand it away, or keep using the new owner.
A later chapter shows how to lend a value without giving it up. For now, just learn to read the "moved value" error.
Your task
The starter does not compile. take_and_measure(text: String) -> (String, usize) should
return the text together with its length in bytes, but it uses text after moving it
into kept. Fix the function so it compiles and returns the right pair. For
example take_and_measure(String::from("hello")) gives ("hello", 5).