Skip to Content
E0382: Borrow of Moved Value

error[E0382]: borrow of moved value

You used a variable after its value moved to a new owner. A String, a Vec and most other types have exactly one owner in Rust. Once ownership passes to another variable or into a function, the old name can’t be used any more.

Quick fix: Pass a reference (&title) instead of the value, or call .clone() if you really need two separate copies.

Code that triggers E0382

src/main.rs
fn print_len(s: String) { println!("{} bytes", s.len()); } fn main() { let title = String::from("Rust errors explained"); print_len(title); println!("{title}"); }

What the compiler says

error[E0382]: borrow of moved value: `title` --> src/main.rs:8:15 | 6 | let title = String::from("Rust errors explained"); | ----- move occurs because `title` has type `String`, which does not implement the `Copy` trait 7 | print_len(title); | ----- value moved here 8 | println!("{title}"); | ^^^^^^^ value borrowed here after move | note: consider changing this parameter type in function `print_len` to borrow instead if owning the value isn't necessary --> src/main.rs:1:17 | 1 | fn print_len(s: String) { | --------- ^^^^^^ this parameter takes ownership of the value | | | in this function = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info) help: consider cloning the value if the performance cost is acceptable | 7 | print_len(title.clone()); | ++++++++ error: aborting due to 1 previous error For more information about this error, try `rustc --explain E0382`.

Why Rust raises E0382

print_len(title) hands ownership of the String to the function’s parameter s. When print_len returns, s goes out of scope and the string is freed. If Rust let line 8 read title, the program would read memory it no longer owns, which is a use-after-free bug. The compiler rejects it at build time instead.

Look at the two places the compiler marks: line 7 is where the value moved (value moved here), and line 8 is where you used it again (value borrowed here after move). The fix always goes in one of those two places: don’t move the value, or don’t use it afterwards.

Only types that implement the Copy trait (integers, floats, bool, char, and tuples of them) are duplicated on assignment instead of moved. That’s why the same code with an i32 compiles without complaint.

How to fix E0382

1. Borrow it instead of moving it

Most of the time a function only needs to read a value. Change the parameter to a reference and pass &title. For string parameters, prefer &str over &String: it accepts a String, a string slice and a string literal.

src/main.rs
fn print_len(s: &str) { println!("{} bytes", s.len()); } fn main() { let title = String::from("Rust errors explained"); print_len(&title); println!("{title}"); }

2. Clone it when you need two owners

If the function genuinely needs its own String, for example to store it in a struct, give it a copy with .clone(). This allocates a second string, so use it when you need the copy, not just to quiet the compiler.

src/main.rs
fn print_len(s: String) { println!("{} bytes", s.len()); } fn main() { let title = String::from("Rust errors explained"); print_len(title.clone()); println!("{title}"); }

3. Give ownership back

A function can return the value it took, handing ownership back to the caller. References made this pattern rare, but you’ll still see it in builder-style APIs.

src/main.rs
fn print_len(s: String) -> String { println!("{} bytes", s.len()); s } fn main() { let title = String::from("Rust errors explained"); let title = print_len(title); println!("{title}"); }

Learn the concept behind it

The Ownership lesson explains the rules behind E0382 with diagrams and more examples. For the official reference, run rustc --explain E0382 or read the E0382 entry in the Rust error index .

Hit a different error? Paste it into the Rust error lookup to find its fix.

Last updated on