🔧 Error Fixes
· 1 min read

Rust: Value Used After Move — How to Fix It


error[E0382]: borrow of moved value: `s`

You used a value after it was moved to another variable or function.

Fix 1: Clone the value

let s = String::from("hello");
let s2 = s.clone();  // Clone instead of move
println!("{}", s);    // Still valid
println!("{}", s2);

Fix 2: Use references

fn print_str(s: &String) {
    println!("{}", s);
}

let s = String::from("hello");
print_str(&s);  // Borrow, don't move
println!("{}", s);  // Still valid

Fix 3: Use after move

// ❌ s is moved
let s = String::from("hello");
let s2 = s;
println!("{}", s);  // Error! s was moved

// ✅ Use s2 instead
println!("{}", s2);

Related: Cargo Cheat Sheet