Move
By the second rule, a value has only one owner at any moment. So what does an ordinary assignment do? With numbers, let b = a; puts a copy into b, and both variables stay usable. Let's try the same with a string:
fn main() {
let s = String::from("bike");
let t = s;
println!("{s} {t}"); // ✗
}error[E0382]: borrow of moved value: `s`The program does not build: the compiler reports that the value of s has been moved. Let's figure out what that means.
The key passes to a new owner
s holds a card with the key to a unit where the bytes bike are kept. The line let t = s; could have three meanings:
- copy the bytes into a new unit: for long text this is expensive, and Rust does not do it silently;
- copy only the card: then the unit has two keys, and at
}both owners would give it back twice; - hand over the key: the card passes to
t, andsowns nothing anymore.
Rust picks the third. Passing ownership from one variable to another is called a move. After let t = s; the string's owner is t, and s has been moved: the compiler forbids using it and does not give the unit back through it.
The bytes bike stay where they are. Only the card on the stack, 24 bytes, is copied, and the old card in s is treated as no longer valid.
The word borrow in the message means that println! wanted to read s, and reading a moved variable is not allowed. The help: hint in the full message suggests let t = s.clone();, which makes a copy of the string if you really need one.
Step through the program and watch the (moved) mark: it shows a variable that has handed over its key.
1fn main() {2 let s = String::from("bike");3 println!("{s}");4 let t = s;5 println!("{t}");6 let n = 5;7 let m = n;8 println!("{n} {m}");9 let u = t;10 println!("{u}");11}
s becomes the owner of the string bike: it holds the key to the unit on the heap.
Numbers are copied
In the same program, let m = n; moves nothing: after it, both n and m are printed. An i32 sits entirely on the stack and has no unit on the heap. Copying its 4 bytes is cheap, and there is nothing to give back to the facility, so no double return can happen.
Rust copies such values on assignment instead of moving them. Integers and floating-point numbers, bool and char behave this way, and they are called Copy types. The full E0382 message says exactly that: s has the type String, which does not implement Copy. That means String is not a Copy type.
Here the key analogy falls short. In real life, the former owner finds out the key is gone only at the door of the unit. In Rust, the compiler finds any use of a moved variable, and the program does not even build: this is a compile error, not a panic at run time. A moved s is not an empty string but a name you can no longer use.
Interviewers like to ask this: "What happens on let s2 = s1; when s1 is a String?" The answer:
- only the card on the stack is copied: the address, the length and the unit size;
- the bytes on the heap stay where they are;
s2becomes the owner, and the compiler no longer lets you uses1;- only
s2will give the unit back, so there is no double return.
Common mistake
A string is moved inside a loop:
fn main() {
let s = String::from("bike");
for i in 0..2 {
let t = s; // ✗
println!("{i}: {t}");
}
}error[E0382]: use of moved value: `s`On the first pass, let t = s; takes the string, and at the end of the pass t goes out of scope and the string is destroyed. On the second pass there is nothing left to move, which the compiler notes under the error: the value was moved in the previous iteration of the loop. This message says use rather than borrow: the string is not read but moved again. If each pass needs its own string, create it inside the loop: let t = String::from("bike");.
Key points
let t = s;with aStringmoves ownership: the key passes tot, and the bytes on the heap stay where they are.- You cannot use a moved variable: it is compile error E0382, not a panic.
- Only the last owner gives the unit back, so there is no double return.
- Numbers,
boolandcharare copied on assignment, and the original variable stays usable.