error[E0507]: cannot move out of value which is behind a shared reference
You tried to take ownership of something you only borrowed. Through a &T you can read the data, but you can’t move parts of it out, because the real owner still expects them to be there.
Quick fix: Return &str (a reference) instead of String, clone the field, or use Option::take when you have mutable access.
Code that triggers E0507
struct Config {
name: String,
}
fn take_name(config: &Config) -> String {
config.name
}
fn main() {
let config = Config { name: String::from("prod") };
println!("{}", take_name(&config));
}What the compiler says
error[E0507]: cannot move out of `config.name` which is behind a shared reference
--> src/main.rs:6:5
|
6 | config.name
| ^^^^^^^^^^^ move occurs because `config.name` has type `String`, which does not implement the `Copy` trait
|
help: consider cloning the value if the performance cost is acceptable
|
6 | config.name.clone()
| ++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0507`.Why Rust raises E0507
config is a shared reference, &Config. You can read through it, but you don’t own the struct it points to. Returning config.name tries to move the String out of that struct, which would leave the caller’s Config with a missing field. Rust refuses.
You’ll meet the same error when you move out of a Vec by index (let s = names[0]; with String elements), dereference a reference to a non-Copy value (*some_ref), or match on a borrowed enum and bind its fields by value.
How to fix E0507
1. Return a reference instead
If the caller only needs to read the name, return a &str that borrows from the struct. Nothing is copied and nothing moves.
struct Config {
name: String,
}
fn name_of(config: &Config) -> &str {
&config.name
}
fn main() {
let config = Config { name: String::from("prod") };
println!("{}", name_of(&config));
}2. Clone the field
If the caller needs its own String, clone it. This is the fix the compiler suggests, and it’s right when you really need an independent copy.
struct Config {
name: String,
}
fn take_name(config: &Config) -> String {
config.name.clone()
}
fn main() {
let config = Config { name: String::from("prod") };
println!("{}", take_name(&config));
}3. Take the value out with Option::take
When you have mutable access (&mut) and want to move the value out, store it as an Option<String> and call .take(), which leaves None behind. std::mem::take does the same for any type with a Default value, leaving the default in its place.
struct Config {
name: Option<String>,
}
fn take_name(config: &mut Config) -> Option<String> {
config.name.take()
}
fn main() {
let mut config = Config { name: Some(String::from("prod")) };
println!("{:?}", take_name(&mut config));
println!("{:?}", config.name);
}Related errors
- E0505: cannot move out of value because it is borrowed
- E0382: borrow of moved value
- E0596: cannot borrow as mutable, as it is not declared as mutable
Learn the concept behind it
The References and Borrowing lesson explains the rules behind E0507 with diagrams and more examples. For the official reference, run rustc --explain E0507 or read the E0507 entry in the Rust error index .
Hit a different error? Paste it into the Rust error lookup to find its fix.