Sub topic 7
Fixing use of moved values
This chapter assumes you can read a basic rustc diagnostic (error code, message, labeled spans) and know the first rules of ownership: each value has one owner, and assigning or passing a value can transfer that ownership. The error here is E0382, "borrow of moved value" or "use of moved value".
Why the error happens
Types that own heap data, such as String and Vec, are moved when you assign them, pass them to a function, or return them. After a move, the old binding is no longer valid. Simple types like i32 implement Copy, so they are duplicated instead and never trigger this error.
fn main() {
let name = String::from("Ferris");
let greeting = name;
println!("{name} {greeting}");
}rustc reports error[E0382]: borrow of moved value: name. It labels three places: where name was declared (with a note that String does not implement Copy), where the value moved, and where you used it afterward.
Reading the diagnostic
Look at the label on the move site. It often says "value moved here". If the move happens inside a function call, the note says the function "takes ownership of the receiver" or names the parameter. Often the help: line already proposes a fix, such as .clone() or borrowing with &.
Choosing a fix
You have three options. Pick based on what the code needs.
| Fix | Use it when | Cost |
|---|---|---|
| Borrow with `&` | The later code only reads the value | None |
| Clone with `.clone()` | Both bindings truly need to own the data | Allocates and copies |
| Restructure | The move was avoidable, or ownership should end | Requires editing the design |
Fix 1: borrow
If a function only reads its argument, change the signature to take a reference.
fn length(s: &String) -> usize {
s.len()
}
fn main() {
let name = String::from("Ferris");
let n = length(&name);
println!("{name} has {n} bytes");
}In real code, prefer &str over &String for read-only parameters, because it accepts more argument types.
Fix 2: clone
When two owners are genuinely needed, clone explicitly.
fn main() {
let name = String::from("Ferris");
let greeting = name.clone();
println!("{name} {greeting}");
}Fix 3: restructure
Moves in loops are a common case. This fails because the first iteration consumes label:
fn consume(s: String) {
println!("{s}");
}
fn main() {
let label = String::from("item");
for _ in 0..2 {
consume(label);
}
}The note reads "value moved here, in previous iteration of loop". Pass a borrow if consume only reads, or create the value inside the loop if each call needs its own:
fn consume(s: &str) {
println!("{s}");
}
fn main() {
let label = String::from("item");
for _ in 0..2 {
consume(&label);
}
}Common mistakes
- Reaching for
.clone()first. It silences the error but hides a design choice and can copy large data. - Borrowing something that must outlive the current scope. That trades E0382 for a lifetime error.
- Forgetting that method calls like
into_iter()orunwrap()takeselfand move the receiver. Check the note on the move site.
Exercise
In let a = vec![1, 2, 3]; let b = a; println!("{}", a.len());, why does the code fail, and what are two ways to fix it?
Show answer
The assignment moves the Vec from a to b, so a is invalid and a.len() triggers E0382. Fix it by borrowing (let b = &a;) if b only needs to read, or by cloning (let b = a.clone();) if both need to own a separate copy.
Exercise
Why does let x = 5; let y = x; println!("{x}"); compile without error?
Show answer
i32 implements Copy, so the assignment duplicates the value instead of moving it. Both x and y stay valid.