Sub topic 1
Moves: what happens when you assign a value
Prerequisites
This chapter assumes you know basic Rust syntax (structs, enums, functions, traits), the basics of ownership and borrowing, and how to read compiler errors from Cargo. It covers only the first step of the plan. The Copy and Clone traits come in later chapters.
Assignment has two outcomes
In Rust, let b = a; does one of two things, and the type of a decides which.
- For simple types such as
i32, the value is duplicated. Bothaandbstay usable. - For types that own a resource, such as
String, the value is moved.bbecomes the new owner andacan no longer be used.
Both outcomes copy bytes. The difference is what the compiler lets you do with the original binding afterward.
fn main() {
let a = 5;
let b = a;
println!("{a} {b}");
let s = String::from("hi");
let t = s;
// println!("{s}"); // error: borrow of moved value: `s`
println!("{t}");
}The first half prints 5 5. The second half prints hi. If you remove the comment marks on the println! that uses s, the compiler rejects the program.
What a move does
A String is a small struct on the stack with three fields: a pointer to heap memory, a length, and a capacity. The text itself lives on the heap. The statement let t = s; copies those three fields into t. It does not copy the heap text.
If s stayed valid, two owners would point at the same heap buffer. Both would try to free it when they went out of scope, which is a double free. Rust prevents this by marking s as moved-from. The compiler tracks this statically, so a move has no runtime check and no extra cost.
Where moves happen
Assignment is not the only place. A move occurs whenever a value is used by value:
let t = s;movessintot.- Passing an argument, as in
consume(s), movessinto the function's parameter. - Returning a value moves it out to the caller.
- Pushing into a collection, as in
v.push(s), movessinto the vector.
After any of these, the original binding is off limits.
fn consume(text: String) -> usize {
text.len()
}
fn main() {
let s = String::from("hello");
let n = consume(s);
println!("{n}");
// println!("{s}"); // error: borrow of moved value: `s`
}The function takes ownership, and the String is freed when consume returns. The caller cannot use s afterward.
Reading the error message
When you use a moved value, the compiler reports error E0382. It usually points to three places: where the value was declared, where it moved, and where you tried to use it. It also often notes that the type "does not implement the Copy trait". That note explains why a move happened here and not a silent duplication.
fn main() {
let names = vec![String::from("Ada"), String::from("Grace")];
let moved = names;
println!("{}", moved.len());
// println!("{}", names.len()); // error[E0382]: borrow of moved value: `names`
}Ways to keep using a value
If you need the original after handing it off, you have three options:
- Borrow it with
&s, so the callee reads without taking ownership. - Duplicate it explicitly with
.clone(). - Use a type that duplicates implicitly, which is the
Copycase.
The next chapters cover the second and third options. Borrowing is the default choice when a function only needs to read.
Exercises
Exercise
Why does let t = s; make s unusable when s is a String, but not when s is an i32?
Show answer
A String owns a heap buffer. If both s and t stayed valid, both would free that buffer, causing a double free. So Rust moves ownership to t and invalidates s. An i32 owns no heap resource and implements Copy, so assignment duplicates it and both bindings remain valid.
Exercise
In this code, which line fails to compile and why? let v = vec![1, 2, 3]; let w = v; println!("{}", v.len());
Show answer
The third statement fails. let w = v; moves the vector into w, so v is moved-from. Calling v.len() then triggers E0382, borrow of moved value v. Use w.len(), or write let w = &v; to borrow instead.