Use of moved value
Quick Answer
Clone the value if you need two owners, or restructure to pass a reference (&T) instead of transferring ownership.
The most common Rust beginner error. A value was moved into another binding or function, and then used again after the move. Rust's ownership model allows only one owner at a time — once moved, the original binding is invalid.
- 1Passing a String/Vec to a function that takes ownership
- 2Assigning a non-Copy type to a second binding
- 3Moving a value into a closure that outlives the original binding
Fix 1
Clone if you need two owners
fn main() {
let s = String::from("hello");
let s2 = s.clone(); // explicit copy
println!("{}", s); // still valid
println!("{}", s2);
}Why this works
clone() creates a deep copy, giving each binding its own owned value. Use sparingly — prefer references for read-only access.
Fix 2
Pass a reference instead of taking ownership
fn print_len(s: &str) { // borrows, does not take ownership
println!("{}", s.len());
}
fn main() {
let s = String::from("hello");
print_len(&s); // borrow
println!("{}", s); // still valid
}Why this works
&str / &T parameters borrow the value without moving it, leaving the caller's binding intact.
fn main() {
let s = String::from("hello");
let _s2 = s; // moved here
println!("{}", s); // error[E0382]: borrow of moved value
}// Run: rustc --explain E0382 // for the full compiler explanation with examples
Rust Compiler Error Index
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev