use of moved value: `...`
The “use of moved value” error in Rust means you tried to use a value after it was moved to another owner. Rust’s ownership system ensures each value has exactly one owner, and when ownership transfers (moves), the original variable becomes invalid.
What It Means
In Rust, assigning a value to another variable or passing it to a function transfers ownership by default for types that do not implement the Copy trait. After the move, the original variable cannot be used because it no longer owns the data. The compiler prevents this at compile time to avoid dangling pointers and double-free bugs.
Why It Happens
- You assigned a value to another variable, moving ownership, then used the original.
- You passed a value to a function and then tried to use it again.
- You moved a value into a closure and later used it outside the closure.
- You iterated over a collection with
for item in collection(consumes it) instead offor item in &collection. - You tried to use a variable after pushing it into a
Vecor similar container.
How to Fix It
1. Clone the data when you need both copies
For types that implement Clone, you can create a copy:
let s1 = String::from("hello");
let s2 = s1.clone(); // s1 is still valid
println!("{} {}", s1, s2);2. Borrow instead of moving
Use references to pass data without transferring ownership:
fn print_length(s: &String) {
println!("{}", s.len());
}
let name = String::from("Alice");
print_length(&name); // borrow, not move
println!("{}", name); // still valid
3. Implement Copy for simple types
Types like integers and booleans implement Copy and are automatically duplicated:
let x = 42;
let y = x; // x is copied, not moved
println!("{} {}", x, y); // both are valid
You can implement Copy for your own types if they only contain Copy fields:
#[derive(Debug, Clone, Copy)]
struct Point {
x: i32,
y: i32,
}4. Iterate by reference
When iterating over a collection you want to use again, iterate by reference:
let items = vec![1, 2, 3];
// This moves the Vec:
for item in items {
println!("{}", item);
}
// println!("{:?}", items); // error: use of moved value
// Fix: iterate by reference
let items = vec![1, 2, 3];
for item in &items {
println!("{}", item);
}
println!("{:?}", items); // works
5. Return ownership from functions
If a function needs to take ownership but you need the value back, return it:
fn consume_and_return(s: String) -> String {
println!("{}", s);
s // return ownership back
}
let name = String::from("Alice");
let name = consume_and_return(name);
println!("{}", name); // still valid
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro