Skip to content
use of moved value: `...`

use of moved value: `...`

DodaTech 3 min read

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 of for item in &collection.
  • You tried to use a variable after pushing it into a Vec or 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

What is the difference between Copy and Clone in Rust?
Copy is an implicit bitwise copy that happens on assignment. Clone is an explicit method call (.clone()) that can implement custom copying logic. Every Copy type must also implement Clone, but not every Clone type is Copy.
Does move semantics affect performance?
Moves in Rust are cheap. They perform a shallow bitwise copy of the stack representation (like a pointer and length for String), not a deep copy of heap data. Only .clone() triggers a deep copy of heap-allocated content.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro