Sub topic 4
Moves: transferring ownership
This chapter assumes you know basic Rust syntax, can build and run a program with cargo, and know the three ownership rules: every value has one owner, there is only one owner at a time, and the value is dropped when the owner goes out of scope.
What a move is
When you assign an owned value to another variable, Rust transfers ownership instead of copying the data. This transfer is called a move. After the move, the original variable is no longer valid.
fn main() {
let s1 = String::from("hello");
let s2 = s1;
// println!("{s1}"); // error: borrow of moved value: `s1`
println!("{s2}");
}A String is a small struct on the stack that holds a pointer, a length, and a capacity. The text itself lives on the heap. The move copies only those three stack fields. If s1 stayed valid, both variables would point to the same heap buffer. Both would try to free it at scope end, which is a double free. Invalidating s1 guarantees that only one owner frees the buffer.
How the compiler tracks a move
The compiler follows each variable through the function. After let s2 = s1;, it treats s1 as uninitialized. Any later read of s1 is a compile error, not a run-time crash.
Where moves happen
Assignment is not the only place. A move also happens when you:
- pass a value to a function by value
- return a value from a function
- put a value into a struct, enum, or collection
- capture a value in a closure declared with
move
fn main() {
let name = String::from("Ferris");
let names = vec![name];
// println!("{name}"); // error: borrow of moved value: `name`
println!("{:?}", names);
}Moves in conditions and loops
The compiler is conservative. If a value is moved on only one branch, it counts as possibly moved afterward.
fn main() {
let s = String::from("data");
let keep = true;
if keep {
let t = s;
println!("{t}");
}
// println!("{s}"); // error: s may have been moved
}A move inside a loop body is also an error unless the variable is reassigned before the next iteration. Otherwise the second iteration would use a moved value.
Reassigning a moved variable
A moved variable declared with mut can be given a new value. It becomes valid again.
fn main() {
let mut s = String::from("first");
let t = s;
s = String::from("second");
println!("{s} {t}");
}When you assign over a variable that still holds a value, Rust drops the old value first.
Common errors
| Error message | Likely cause | Fix |
|---|---|---|
| `borrow of moved value` | You used a variable after passing or assigning it | Borrow with `&`, or clone |
| `use of moved value` | A move happened earlier, perhaps in a loop | Restructure, or clone before the move |
| `cannot move out of index` | You tried `let x = v[0];` on a `Vec<String>` | Borrow with `&v[0]`, or clone |
Exercise
Why does the compiler reject using s1 after let s2 = s1; when s1 is a String?
Show answer
The assignment moves ownership of the String to s2, so s1 is invalid. If both stayed valid, both would free the same heap buffer when they went out of scope, causing a double free.
Exercise
In let v = vec![String::from("a")]; let x = v[0];, why does the compiler complain, and how do you fix it?
Show answer
Indexing would move a String out of the vector and leave it with an invalid element. Borrow with let x = &v[0]; or duplicate with let x = v[0].clone();.