E0382
RustERRORCommonOwnership

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.

What this means

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.

Why it happens
  1. 1Passing a String/Vec to a function that takes ownership
  2. 2Assigning a non-Copy type to a second binding
  3. 3Moving a value into a closure that outlives the original binding

Fix 1

Clone if you need two owners

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

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.

Code examples
Triggerrust
fn main() {
    let s = String::from("hello");
    let _s2 = s;          // moved here
    println!("{}", s);   // error[E0382]: borrow of moved value
}
rustc --explainrust
// Run: rustc --explain E0382
// for the full compiler explanation with examples
Same error in other languages
Sources
Official documentation ↗

Rust Compiler Error Index

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Rust errors