
You write five lines of Rust, hit build, and get this:
error[E0382]: borrow of moved value: `s1`
The code looks fine. You only assigned one variable to another. This post explains what Rust is doing, why it does it, and the four ways to fix it.
The code that fails
fn main() {
let s1 = String::from("hello");
let s2 = s1;
println!("{s1} {s2}"); // error[E0382]
}
In most languages s2 = s1 gives you two names for the same thing. In Rust it moves the value. After the move, s1 is no longer usable, and the compiler stops you from touching it.
Why Rust moves the value
A String has two parts. A small header on the stack (pointer, length, capacity) and the actual bytes on the heap.

let s2 = s1 copies only the header. Now two headers point at the same heap bytes. If both were valid, both would try to free that memory when they go out of scope. That is a double free.
Rust avoids it with three rules:
- Every value has one owner.
- There is only one owner at a time.
- When the owner goes out of scope, the value is dropped and its memory is freed.
So the assignment hands ownership to s2 and marks s1 as moved. One owner, one free, no bug. And it all happens at compile time, with no garbage collector.
Why integers do not move
This works fine:
fn main() {
let x = 5;
let y = x;
println!("{x} {y}"); // 5 5
}
i32 implements the Copy trait. Copying it is just copying a few bytes, and there is no heap memory to free twice, so Rust copies instead of moving. Integers, floats, bool, char, and tuples or arrays made only of Copy types behave this way.
It is not about stack versus heap. A struct you define lives on the stack too, but it moves unless you add #[derive(Clone, Copy)], and you can only do that if all its fields are Copy. A struct holding a String can never be Copy.
Function calls move too
Passing a value to a function is the same as assigning it:
fn print_it(s: String) {
println!("{s}");
} // s is dropped here
fn main() {
let name = String::from("ferris");
print_it(name);
print_it(name); // error[E0382]: use of moved value
}
The first call moves name into print_it, and the string is freed when that function ends. There is nothing left for the second call.
Fix 1: borrow with a reference
Most of the time the function only needs to read the value. Give it a reference instead:
fn print_it(s: &str) {
println!("{s}");
}
fn main() {
let name = String::from("ferris");
print_it(&name);
print_it(&name); // fine, name still owns the string
}
&name lends the value without giving it away. Taking &str instead of &String also lets the function accept string literals.
Fix 2: borrow mutably
If the function needs to change the value, use &mut:
fn shout(s: &mut String) {
s.push('!');
}
fn main() {
let mut name = String::from("ferris");
shout(&mut name);
println!("{name}"); // ferris!
}
You can have many & borrows or one &mut borrow at a time, never both. That rule is what stops data races at compile time. The borrow checker page walks through the errors it produces.
Fix 3: clone when you really need two copies
fn main() {
let s1 = String::from("hello");
let s2 = s1.clone();
println!("{s1} {s2}"); // both valid
}
clone() copies the heap bytes too, so it costs memory and time. It is the right choice when two parts of the program truly need their own copy. It is the wrong choice when you just want the error to go away. Try a borrow first.
Fix 4: move it and give it back
Sometimes a function should own the value, for example a constructor or a builder. Return the value when you are done with it:
fn add_suffix(mut s: String) -> String {
s.push_str(".txt");
s
}
fn main() {
let file = String::from("report");
let file = add_suffix(file);
println!("{file}"); // report.txt
}
A common trap: moving out of a loop
fn main() {
let names = vec![String::from("a"), String::from("b")];
for n in names {
println!("{n}");
}
println!("{}", names.len()); // error: names was moved by the loop
}
for n in names consumes the vector. Use for n in &names to borrow it and keep the vector afterwards.
Which fix to pick
| Situation | Use |
|---|---|
| Function only reads the value | &T or &str / &[T] |
| Function changes the value | &mut T |
| Two owners need separate data | .clone() |
| Function should keep the value | Pass by value (move) |
| Several owners share one value | Rc<T> or Arc<T> |
Next steps
Once moves make sense, the next errors you meet are about references that outlive their data. These pages go further:
- Ownership
- References in Depth
- Understanding the Borrow Checker
- Validating References with Lifetimes
- Shared Ownership with Rc and Arc
The official Rust Book chapter on ownership covers the same ground with more examples.