Loading RustViz Engine...
Decode exact rustc diagnostics, understand why the compiler rejected the code, and master idiomatic fixes.
error[E0382]: borrow of moved value: `data`
--> src/main.rs:5:20
|
2 | let data = String::from("Rustacean");
| ---- move occurs because `data` has type `String`, which does not implement `Copy`
3 | process_data(data);
| ---- value moved here
4 |
5 | println!("{}", data);
| ^^^^ value borrowed here after movefn process_data(s: String) {
println!("Processing: {}", s);
}
fn main() {
let data = String::from("Rustacean");
process_data(data); // Ownership is transferred!
println!("Length: {}", data.len()); // E0382 error!
}// Fix Option 1: Pass an immutable reference instead of full ownership
fn process_data(s: &str) {
println!("Processing: {}", s);
}
fn main() {
let data = String::from("Rustacean");
process_data(&data); // Borrowing &data
println!("Length: {}", data.len()); // Works perfectly!
}Heap-backed types in Rust do not implement Copy. Passing `data` by value moves ownership into the function, invalidating the original binding on the stack. Fix by borrowing (`&data`) or explicitly calling `.clone()` if duplicate heap allocation is desired.