E0505
RustERRORCommonOwnership
Cannot move out of value because it is borrowed
Quick Answer
Ensure all borrows of a value end before moving it, or restructure to clone the value instead of moving.
What this means
An attempt was made to move ownership of a value while a borrow of that value (or a field of it) is still alive. Moving a borrowed value would leave the existing reference dangling.
Why it happens
- 1Passing a struct by value to a function while holding a reference to one of its fields
- 2Returning a local variable by value while a reference to a field still exists
- 3Moving a capture in a closure while an outer borrow is active
Fix
End the borrow before moving
End the borrow before moving
struct Config { name: String }
fn use_config(c: Config) { println!("{}", c.name); }
fn main() {
let config = Config { name: "app".to_string() };
{
let _name_ref = &config.name; // borrow
println!("{}", _name_ref);
} // borrow ends
use_config(config); // move — now safe
}Why this works
Scoping the borrow inside a block ensures it drops before the move, satisfying the borrow checker.
Code examples
Triggerrust
let s = String::from("hello");
let r = &s;
drop(s); // error[E0505]: cannot move out of s because it is borrowed
println!("{}", r);Sources
Official documentation ↗
Rust Compiler Error Index
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev