E0505 Cannot Move Out Fix
A borrow of a value is still live - and still has a use ahead of it - at the line where the value itself is moved. This guide separates five shapes that produce E0505 and matches each one to the change that ends or avoids the overlap: finish with the borrow first, copy or clone out of it, or let a Copy type turn the move into a copy.
Written November 23, 2026 · Tested with Rust 1.99.1 · 25 min read
- The rule underneath: a value cannot move while a borrow of it is still live
- How to read the diagnostic
- The first failure: a vector moved while a borrow of one element is live
- The second failure: a struct moved while a borrow of one field is live
Written November 23, 2026
Tested with Rust 1.99.1
What this guide is
Error E0505 is a blocked move. The error codes index defines it in one line: a value was moved out while it was still borrowed. The move itself is an ordinary one - let moved = v, passing s to a function that takes ownership, or a move closure capturing its environment - and the value being moved is owned by a local binding, not sitting behind a reference. What blocks it is a borrow: some reference to that same value, to one of its elements, or to one of its fields was created earlier, is still live at the move line, and is used again afterward. Moving the value would drop or relocate what that reference points at, so the compiler refuses the move until the borrow is over.
The five forms in this guide each reproduce E0505 with a small, complete program, and each holds the borrow open by a different route. A borrow of one element of a vector while the whole vector is moved. A borrow of one struct field while the whole struct is moved. A shared borrow of a string while the string is passed by value to a function. A slice borrow of a string while the string itself is moved. And a shared borrow of a string while a move closure tries to take it. In each family the failing program is small, the full compiler output is shown, and the fix ends the overlap rather than fighting it: the borrow's last use moves before the move, or the borrowed part is copied or cloned out so no borrow survives at all.
Two neighboring codes are not E0505 and are worked only in the traps and the decision summary. When the move tries to take a value out from behind a reference - let moved = *r where r is a &String - the error is E0507, cannot move out of borrowed content, because the code does not own the value behind the reference in the first place. And when the move already succeeded and later code tries to use or borrow the original binding, the error is E0382, borrow of moved value, because there is no live borrow blocking anything - the value is simply gone. E0505 is the remaining case: the value is still there, the move is otherwise legal, and only the still-live borrow stands in the way.
Build every program in this guide with rustc --edition 2021. In the compiler transcripts below, the file names are the bare names shown on the command line.
The rule underneath: a value cannot move while a borrow of it is still live
A move transfers ownership. The Rust Book chapter on ownership states the rules the move obeys: each value has an owner, there can only be one owner at a time, and when the owner goes out of scope the value is dropped. Assigning or passing a non-Copy value such as a String or a Vec moves it - the old binding is invalidated and the new one owns the value, including the heap data behind it. A reference is a borrow: it refers to a value owned by some other variable, without taking ownership of it, as the Rust Book chapter on references and borrowing puts it. A borrow promises its holder that the value it points at will stay where it is and stay alive for as long as the borrow is used.
A move breaks that promise in both directions at once. The value leaves its old place, so a reference into the old place - to the value, to an element of it, to a field of it - would point at a binding that no longer owns anything. And when the new owner eventually drops the value, the borrowed data is freed under a reference that still claims it is valid. So the compiler allows the move only when no borrow of the value is still live.
What makes a borrow "still live" is use, not scope alone. The Book states the scope rule directly: a reference's scope starts where it is introduced and continues through the last time that reference is used. This is the non-lexical-lifetimes behavior every fix in this guide relies on. A borrow whose last use is on the line before the move is already over at the move, and the identical move compiles. A borrow with one more use waiting below the move - a println!, a return, a later read - is live across the move, and the move fails. That is why the diagnostic always shows three points, not one: where the value was borrowed, where the move occurs, and where the borrow is later used. Delete or move that later use and the error goes with it.
The rule follows the value as a whole, not just the borrowed part. Borrowing one element of a vector or one field of a struct still blocks moving the whole container, because the move takes the borrowed part with it - the first two families show the borrow label naming an element and a field while the header names the whole vector and the whole struct. There is no partial-move exception here while a borrow is live: a disjoint-field move that would be legal with no borrow outstanding is still a move of a borrowed value's part, and the borrow has to end first.
One class of value never hits this error at all. For a type that implements Copy, the "move" is a copy: the original stays valid, the borrow keeps pointing at it, and both can be used afterward. The error codes index lists implementing Copy as one of its three fixes for E0505, and the traps section shows the by-value family compiling unchanged once its type derives Copy. That fix is only available when every field is itself Copy - a String or a Vec field rules it out - which is why most families below use the ordering and copy-out fixes instead.
How to read the diagnostic
The header names the moved value and the reason in one line: "cannot move out of" the value "because it is borrowed". Read the value first, because it tells you what is being moved - v is a whole vector, u is a whole struct, s is a whole string - even when the borrow itself is only of one element or one field of it. Three labeled lines then map the overlap. The line ending "borrow of x occurs here" marks where the live borrow was created; for an element or field borrow it names the part, as in "borrow of u.name occurs here". The caret line repeats the move with the label "move out of x occurs here" - in the closure family an extra label, "move occurs due to use in closure", shows the line inside the closure that forces the capture to be a move. And a later line is marked "borrow later used here" - that use is the reason the borrow is still live, and it is the line every fix in this guide moves, removes, or works around.
Most E0505 diagnostics in this guide also carry one help: line: "consider cloning the value if the performance cost is acceptable", with a rewrite that borrows a clone instead - &s.clone(), &v.clone()[0], &u.name.clone(). That rewrite is real and it compiles, but read what it does before taking it: the borrow then points at a temporary clone, not at the original value, and cloning a whole vector to borrow one element allocates a second vector to read one string. It is the right fix only when the code genuinely needs an independent value that outlives the move. Where the borrow only needs one last read before the move, reordering is free and cloning is waste - the families below reorder first and reserve cloning for the copy-out form in the traps, where the clone is of the borrowed part alone.
The first failure: a vector moved while a borrow of one element is live
first borrows v[0] - one element - the next line moves the whole vector, and the println! after that still reads through first. That later read is what keeps the borrow alive across the move:
fn main() {
let v = vec![String::from("alpha"), String::from("beta")];
let first = &v[0];
let moved = v;
println!("{}, {:?}", first, moved);
}
$ rustc --edition 2021 fail_vec_elem.rs -o fail_vec_elem
error[E0505]: cannot move out of `v` because it is borrowed
--> fail_vec_elem.rs:4:17
|
2 | let v = vec![String::from("alpha"), String::from("beta")];
| - binding `v` declared here
3 | let first = &v[0];
| - borrow of `v` occurs here
4 | let moved = v;
| ^ move out of `v` occurs here
5 | println!("{}, {:?}", first, moved);
| ----- borrow later used here
|
help: consider cloning the value if the performance cost is acceptable
|
3 | let first = &v.clone()[0];
| ++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0505`.
The borrow is of one element, but the header names v - moving the vector moves the element with it, so the element's borrow blocks the whole move. The fix needs no new construct - read the borrow for the last time before the move, and the borrow ends at that read:
fn main() {
let v = vec![String::from("alpha"), String::from("beta")];
let first = &v[0];
println!("{}", first);
let moved = v;
println!("{:?}", moved);
}
$ rustc --edition 2021 fix_vec_elem.rs -o fix_vec_elem
$ ./fix_vec_elem
alpha
["alpha", "beta"]
The output is the pair the failing program was trying to print, in the only order that works: alpha, the borrowed element read while the borrow was live, then ["alpha", "beta"], the vector after the move the borrow no longer blocks. Nothing about the borrow changed - it is the same &v[0] - only its last use moved above the move.
The second failure: a struct moved while a borrow of one field is live
Here the borrow is of one field and the move is of the whole struct. name_ref borrows u.name, the next line moves u, and the final println! still reads name_ref:
struct User {
name: String,
id: u32,
}
fn main() {
let u = User {
name: String::from("Ada"),
id: 7,
};
let name_ref = &u.name;
let moved = u;
println!("{}, {}", name_ref, moved.id);
}
$ rustc --edition 2021 fail_struct_field.rs -o fail_struct_field
error[E0505]: cannot move out of `u` because it is borrowed
--> fail_struct_field.rs:12:17
|
7 | let u = User {
| - binding `u` declared here
...
11 | let name_ref = &u.name;
| ------- borrow of `u.name` occurs here
12 | let moved = u;
| ^ move out of `u` occurs here
13 | println!("{}, {}", name_ref, moved.id);
| -------- borrow later used here
|
help: consider cloning the value if the performance cost is acceptable
|
11 | let name_ref = &u.name.clone();
| ++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0505`.
The borrow label is the precise one - "borrow of u.name" - while the header still names the whole struct u, because the move takes every field, borrowed or not. Note the compiler's help here clones just the field, &u.name.clone(), which is the cheap end of the cloning fixes. The fix below is the first family's fix at whole-struct scale: the borrow's last use moves above the move:
struct User {
name: String,
id: u32,
}
fn main() {
let u = User {
name: String::from("Ada"),
id: 7,
};
let name_ref = &u.name;
println!("{}", name_ref);
let moved = u;
println!("{}, {}", moved.name, moved.id);
}
$ rustc --edition 2021 fix_struct_field.rs -o fix_struct_field
$ ./fix_struct_field
Ada
Ada, 7
The output is Ada, the borrowed field read before the move, then Ada, 7 - the struct's fields read from their new owner after it. The id field was never borrowed, but that buys nothing while the move is blocked: the move is of the whole value, so one live field borrow is enough to stop it, and ending that one borrow is enough to free it.
The third failure: a value passed by value while a shared borrow of it is live
eat takes its argument by value, so calling eat(s) is a move of s. r borrows s on the line before the call, and the println! after the call still reads through r:
fn eat(s: String) {
println!("ate {}", s);
}
fn main() {
let s = String::from("hello");
let r = &s;
eat(s);
println!("{}", r);
}
$ rustc --edition 2021 fail_by_value.rs -o fail_by_value
error[E0505]: cannot move out of `s` because it is borrowed
--> fail_by_value.rs:8:9
|
6 | let s = String::from("hello");
| - binding `s` declared here
7 | let r = &s;
| -- borrow of `s` occurs here
8 | eat(s);
| ^ move out of `s` occurs here
9 | println!("{}", r);
| - borrow later used here
|
help: consider cloning the value if the performance cost is acceptable
|
7 | let r = &s.clone();
| ++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0505`.
This is the shape in the error codes index's own example - a function that takes ownership on one side of the borrow, and a use of the borrow on the other side. The caret is a single column under the argument, because the move is the argument passing itself. The fix is the same reorder: the borrow's last use moves above the call, and the call then moves a value nobody is borrowing:
fn eat(s: String) {
println!("ate {}", s);
}
fn main() {
let s = String::from("hello");
let r = &s;
println!("{}", r);
eat(s);
}
$ rustc --edition 2021 fix_by_value.rs -o fix_by_value
$ ./fix_by_value
hello
ate hello
The output is hello, read through the borrow, then ate hello from inside the function that now owns the string. Where the function's signature is yours to change, the other index fix applies instead: take &String rather than String, and there is no move to block - the borrow and the call coexist, at the cost of the caller keeping ownership it may not want. Where the signature is fixed and the borrow's data must survive the call, that is the one case for the compiler's cloning help: borrow a clone, and pay for a second string.
The fourth failure: a string moved while a slice borrow of it is live
first_word is a &str - a slice borrow of the first five bytes of s - the next line moves s, and the println! after that still reads the slice:
fn main() {
let s = String::from("hello world");
let first_word = &s[..5];
let moved = s;
println!("{}, {}", first_word, moved);
}
$ rustc --edition 2021 fail_string_slice.rs -o fail_string_slice
error[E0505]: cannot move out of `s` because it is borrowed
--> fail_string_slice.rs:4:17
|
2 | let s = String::from("hello world");
| - binding `s` declared here
3 | let first_word = &s[..5];
| - borrow of `s` occurs here
4 | let moved = s;
| ^ move out of `s` occurs here
5 | println!("{}, {}", first_word, moved);
| ---------- borrow later used here
|
help: consider cloning the value if the performance cost is acceptable
|
3 | let first_word = &s.clone()[..5];
| ++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0505`.
A slice has no storage of its own - it is a pointer and a length into the string's buffer - so it is a borrow of the whole string for move purposes, even though it views only part of it. The compiler's help is at its worst here: &s.clone()[..5] clones the entire string so the slice can point into the discarded clone. The reorder costs nothing:
fn main() {
let s = String::from("hello world");
let first_word = &s[..5];
println!("{}", first_word);
let moved = s;
println!("{}", moved);
}
$ rustc --edition 2021 fix_string_slice.rs -o fix_string_slice
$ ./fix_string_slice
hello
hello world
The output is hello, the slice read while its borrow was live, then hello world, the whole string from its new owner. When the slice's text is genuinely needed after the move - stored, returned, passed onward - copy the text, not the string: let first_word = s[..5].to_string(); owns a new small string holding just the slice, no borrow survives, and the move is free. That is the copy-out fix at slice granularity, and its cost is the size of the slice, not the size of the string.
The fifth failure: a move closure capturing a borrowed value
A move closure takes ownership of every value it uses from its environment, so creating one is a move of those values. Here r borrows s, the move closure tries to take s, and the println! after the closure still reads through r:
fn main() {
let s = String::from("hello");
let r = &s;
let show = move || {
println!("{}", s);
};
show();
println!("{}", r);
}
$ rustc --edition 2021 fail_closure.rs -o fail_closure
error[E0505]: cannot move out of `s` because it is borrowed
--> fail_closure.rs:4:16
|
2 | let s = String::from("hello");
| - binding `s` declared here
3 | let r = &s;
| -- borrow of `s` occurs here
4 | let show = move || {
| ^^^^^^^ move out of `s` occurs here
5 | println!("{}", s);
| - move occurs due to use in closure
...
8 | println!("{}", r);
| - borrow later used here
|
help: consider cloning the value if the performance cost is acceptable
|
3 | let r = &s.clone();
| ++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0505`.
The diagnostic carries a label the other families do not: "move occurs due to use in closure", on the line inside the closure that uses s. The move itself happens where the closure is created, not where it is called - the caret sits on the move keyword line - so reordering around the call does nothing; the borrow has to end before the closure is defined. That is the fix:
fn main() {
let s = String::from("hello");
let r = &s;
println!("{}", r);
let show = move || {
println!("{}", s);
};
show();
}
$ rustc --edition 2021 fix_closure.rs -o fix_closure
$ ./fix_closure
hello
hello
The output is hello twice: once through the borrow, before the closure exists, and once from inside the closure, which now owns the string. The same timing rule governs loops that move a value on a later pass: the move happens at the capture or at the move expression, and any borrow used after that point - including in a later iteration's code - holds the move open until its last use is moved above it.
The fixes in reach order
- The borrow has a last use that can move: move it above the move - the borrow ends at its last use, and the move after it is free. This is the first reach for every family in this guide, and it costs nothing.
- The code only needs the borrowed data, not a live reference into the original: copy it out - a plain read for a
Copyelement,clone()orto_string()on the borrowed part otherwise - and keep no reference at all. The cost is the size of the part copied, stated in the traps below. - The blocked operation is a call whose signature you control: change it to take a reference instead of taking ownership, as the error codes index's first example fix does - there is then no move, and the borrow and the call coexist.
- The value must be both borrowed later and moved now, with no reordering available: clone the whole value for one of the two sides - the compiler's
help:rewrite - and accept the allocation; cloning a container to save one element is the expensive end of this fix. - The type is small, plain data with no heap parts: derive
Copy, and the move becomes a copy that leaves the original - and its borrows - valid. Only available when every field isCopy. - The borrow must genuinely outlive the move - stored in a struct, returned, sent to another thread: the design, not the ordering, is wrong for a plain move; shared ownership such as
Rcis the shape built for that case.
Traps
- Copying a
Copyelement out removes the borrow entirely. This is the first family with ani32vector:v[0]copies the number instead of borrowing its place, so no reference survives to block the move, and the program compiles as written:
fn main() {
let v = vec![10, 20, 30];
let first = v[0];
let moved = v;
println!("{}, {:?}", first, moved);
}
$ rustc --edition 2021 probe_copy_out.rs -o probe_copy_out
$ ./probe_copy_out
10, [10, 20, 30]
10, [10, 20, 30] - the copied element, then the moved vector. The one-character difference from the first family, v[0] instead of &v[0], is the whole fix, and it works because i32 is Copy: reading it duplicates the value rather than borrowing the place it lives in.
- Cloning the borrowed element out works for non-Copy types, at the element's cost. The first family again, with the borrow replaced by a clone of just the borrowed element:fn main() {
let v = vec![String::from("alpha"), String::from("beta")];
let first = v[0].clone();
let moved = v;
println!("{}, {:?}", first, moved);
}
$ rustc --edition 2021 probe_clone.rs -o probe_clone
$ ./probe_clone
alpha, ["alpha", "beta"]
The output is the pair the failing program wanted: alpha, ["alpha", "beta"]. The cost is one cloned string - the element's bytes are allocated and copied once - and in exchange first is an owned value that stays valid whatever happens to the vector. Contrast the compiler's help in the first family's transcript, which clones the whole vector to borrow one element from the clone: same output, a full extra vector allocated. Clone the part you need, not the container it sits in.
- A Copy type is never moved at all. This is the third family with its string replaced by a small Copy struct, and it compiles unchanged - borrow, by-value call, and later use of the borrow all together:
#[derive(Clone, Copy)]
struct Token {
id: u32,
}
fn eat(t: Token) {
println!("ate {}", t.id);
}
fn main() {
let x = Token { id: 42 };
let r = &x;
eat(x);
println!("{}", r.id);
}
$ rustc --edition 2021 probe_copy_type.rs -o probe_copy_type
$ ./probe_copy_type
ate 42
42
The output is ate 42, then 42 read through the borrow after the call. eat(x) copied x rather than moving it, so the original stayed in place and the borrow of it stayed valid - this is the error codes index's third fix, and it is why E0505 only ever names values of non-Copy types.
- Moving out from behind a reference is a different error. Here there is no borrow of the value by a second party and no move of the owning binding - the code tries to move the value out of the reference itself, *r, and the failure is E0507:
fn main() {
let s = String::from("hello");
let r = &s;
let moved = *r;
println!("{}", moved);
}
$ rustc --edition 2021 probe_e0507.rs -o probe_e0507
error[E0507]: cannot move out of `*r` which is behind a shared reference
--> probe_e0507.rs:4:17
|
4 | let moved = *r;
| ^^ move occurs because `*r` has type `String`, which does not implement the `Copy` trait
|
help: consider removing the dereference here
|
4 - let moved = *r;
4 + let moved = r;
|
help: consider cloning the value if the performance cost is acceptable
|
4 - let moved = *r;
4 + let moved = r.clone();
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0507`.
The separation is exact: in E0505 the code owns the value and a live borrow blocks its move - end the borrow and the move proceeds. In E0507 the code never owned the value on this path at all - it holds only a &, and no reordering gives it ownership; the fixes are to work through the reference, clone what it points at, or get the owned value back from its owner. The error codes index defines E0507 as a borrowed value being moved out.
- Using a value after it was moved is the opposite timing, and a third error. Here the move succeeds - nothing borrows s at the move line - and the failure comes after, when the code tries to borrow the binding the move emptied:
fn main() {
let s = String::from("hello");
let moved = s;
let r = &s;
println!("{}, {}", r, moved);
}
$ rustc --edition 2021 probe_e0382.rs -o probe_e0382
error[E0382]: borrow of moved value: `s`
--> probe_e0382.rs:4:13
|
2 | let s = String::from("hello");
| - move occurs because `s` has type `String`, which does not implement the `Copy` trait
3 | let moved = s;
| - value moved here
4 | let r = &s;
| ^^ value borrowed here after move
|
help: consider cloning the value if the performance cost is acceptable
|
3 | let moved = s.clone();
| ++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0382`.
Read the order of the labels to tell the two apart: E0505 is borrow first, move second, borrow used third - the borrow is the obstacle. E0382 is move first, use second - there is no obstacle, the value is gone, and the fix is to not need it after the move, or to clone at the move so the original survives. The error codes index defines E0382 as a variable used after its contents have been moved elsewhere.
Decision summary
- The header says "cannot move out of" a value "because it is borrowed", with labels for where it was borrowed, where the move occurs, and where the borrow is later used: a live borrow blocks an otherwise legal move of an owned value - end the borrow, copy out of it, or clone for one side.
- The borrow label names a part - an element
v[0], a fieldu.name, a slice ofs: the borrow is of the part, but the blocked move is of the whole value, and ending that one part's borrow frees the whole move. - The caret sits on a function argument: passing by value is the move - reorder the borrow's last use above the call, change the signature to take a reference, or clone for the call.
- An extra label says "move occurs due to use in closure": a
moveclosure is the mover, and the move happens where the closure is created - the borrow must end before that line, not before the call. - The header instead says the place "is behind a shared reference" (E0507): the code holds only a borrow of the value and tries to move out of it - no reordering helps; clone, work through the reference, or recover ownership.
- The header instead says "borrow of moved value" (E0382): the move already happened and later code touches the emptied binding - the fix is at the move, not at a borrow.
- No error at all where a borrow and a by-value use coexist: check the type - a
Copytype was copied, not moved, and there was never a move to block.
Checklist
- The header read for the moved value: a whole variable, vector, struct, or string - the move is of the whole, whatever part the borrow names.
- The three labels traced: where the value was borrowed, the move line, and the later use that keeps the borrow live.
- The later use dealt with first: moved above the move, removed, or replaced by a copy or clone of the borrowed part taken before the move.
- The copy-out cost checked: a
Copyread is free, a clone costs the part copied - never clone a whole container to free one element or field. - Closure captures checked at the definition line: a
moveclosure moves its captures when it is created, so borrows must end before it. - The neighboring codes ruled out by the header's wording: a move out from behind a reference is E0507, a use after a completed move is E0382 - neither is fixed by the reordering that fixes E0505.
- The fixed program run, and its output checked against what the failing program was trying to compute.
Related guides on this site
- E0503 Cannot Use Fix
- E0509 Cannot Move Out Fix
- Fix Rust E0507 "cannot move out of ..." — Vec Index, HashMap Index, a Shared Reference, a Struct Field, and a Mutable Reference
- Fix Rust E0502 "cannot borrow as mutable because it is also borrowed as immutable" — End the First Borrow, Split the Work, or Restructure
Sources
- Rust, Error codes index: E0505 - the definition of the error: a value was moved out while it was still borrowed; its example passes a value to a function that takes ownership while a reference to it is held for a later call, and its fixes avoid the move by passing a reference, release the borrow before the move, or implement the Copy trait on the type.
- The Rust Programming Language, References and Borrowing - references borrow a value without owning it, so the value is not dropped when the reference stops being used; and a reference's scope runs from its introduction through its last use, so a borrow whose last use precedes a later move no longer blocks it.
- The Rust Programming Language, What is Ownership? - the ownership rules a move obeys: each value has an owner, there can only be one owner at a time, and the value is dropped when the owner goes out of scope; types that are Copy are copied instead of moved, leaving the original valid.
- Rust, Error codes index: E0507 - the neighboring behind-a-reference error: a borrowed value was moved out; the distinction from E0505 is that the moving code holds only a reference to the value, rather than owning a value that a separate live borrow blocks.
- Rust, Error codes index: E0382 - the neighboring after-the-move error: a variable was used after its contents have been moved elsewhere; the distinction from E0505 is timing - the move completed, and the failure is the later use of the emptied binding, not a borrow blocking the move.
Keep Exploring
The full Rust hub \u2014 ownership, structs and enums, async, and measured comparisons.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.