E0382: use of moved value
In Rust every value has exactly one owner. When you assign a heap-owning value like a `String` or `Vec` to another variable, or pass it to a function by value, ownership *moves*. The old name stops being valid.
This is not the compiler being fussy. It is the mechanism that lets Rust free memory at exactly the right moment without a garbage collector, and it removes use-after-free bugs entirely.
Code that triggers it
fn consume(text: String) {
println!("consumed: {text}");
}
fn main() {
let message = String::from("hello");
consume(message);
println!("{message}"); // error[E0382]
}Code that compiles
fn look(text: &str) {
println!("looked at: {text}");
}
fn main() {
let message = String::from("hello");
look(&message); // borrow instead of moving
println!("{message}"); // still ours
}Borrow with `&` when the function only needs to read the value. Clone with `.clone()` when you genuinely need two independent copies. Move only when the receiver should own the data from then on.
Try both versions
The broken version is loaded below so you can see the real compiler message, then fix it yourself.
Contrast AAA · 16.6:1
15px