E0509 Cannot Move Out Fix
A type with a Drop impl is a single unit: the whole value can move, but no field or payload can be moved out of it. This guide separates five shapes that produce E0509 and matches each one to the change that gets the field's value out legally: borrow it, clone it, or swap a replacement into its place.
Written November 23, 2026 · Tested with Rust 1.99.1 · 27 min read
- The rule underneath: a Drop value is one unit, and only the unit moves
- How to read the diagnostic
- The first failure: a struct field moved into a new binding
- The second failure: a field passed by value to a function
Written November 23, 2026
Tested with Rust 1.99.1
What this guide is
Error E0509 is a blocked move out of a value whose type implements Drop. The error codes index states it in one line: this error occurs when an attempt is made to move out of a value whose type implements the Drop trait. The move itself is an ordinary one - binding a field to a new variable, passing a field to a function by value, binding an enum's payload in a match arm, or destructuring a struct into its fields - and the same code compiles the moment the Drop impl is removed. What blocks it is the destructor: when a value of a Drop type goes out of scope, its drop method runs and may use any of its fields, so the compiler treats the value as a single unit whose parts cannot leave separately. A value with a missing field could not be dropped soundly.
The five forms in this guide each reproduce E0509 with a small, complete program, and each reaches the blocked move by a different route. A named struct field bound to a new variable. The same kind of field passed by value to a function. A tuple struct's .0 field bound on its own. An enum payload bound by value in a match arm. And a struct taken apart by a destructuring pattern that moves one non-Copy field. In each family the failing program is small, the full compiler output is shown, and the fix gets the value out without moving it out: borrow the field instead of taking it, clone it, or - where the field genuinely has to leave - swap a replacement into its place so the value is whole again when it drops.
Four neighboring results are not E0509 and are worked only in the traps and the decision summary. Moving a field out of a struct with no Drop impl compiles - a partial move is legal there - and moving the whole Drop value compiles too, because the unit moves intact. Moving a field out through a reference is E0507, cannot move out of a value which is behind a reference - the subject of the E0507 guide. Using a value after the whole value moved is E0382, borrow of moved value - the subject of the E0382 guide. And two failures that look like this one from the fix side are separated in the traps: naming a field that is private is E0616, and reaching for std::mem::take on a field whose type does not implement Default fails as E0277, a missing trait bound, not as a move error.
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 Drop value is one unit, and only the unit moves
The error codes index gives the reason directly: a struct cannot be dropped if one or more of its fields have been moved, because the implicit destructor that runs when the value goes out of scope may use the fields of the struct. So values whose type implements Drop must be thought of as single units whose fields cannot be moved. The restriction is on moving out of the value, not on the value itself: assigning the whole value to a new variable, passing it whole to a function, or returning it all move the unit intact, destructor and fields together, and all compile. It is also not a restriction on access: a field can be read through a shared reference, mutably borrowed, cloned, or - through a mutable reference to the field - swapped for a replacement value, because in every one of those the field's place is filled again before the destructor could ever see it empty.
That last route is the one the standard library builds for exactly this error. std::mem::take replaces the value behind a &mut with the type's Default and returns the old value; std::mem::replace does the same with a replacement you supply. Neither moves out of the Drop value - each goes through a mutable borrow of the field and leaves a valid value behind. Option::take is the same idea specialized: it replaces an Option field with None and returns what was there. And wrapping the value in std::mem::ManuallyDrop suppresses the automatic destructor call, so the value can be dismantled manually through the wrapper - at the price of owing the destructor call yourself.
The rule is about the Drop impl of the type the value is moved out of, not the moved field's own type. In the first family below, the field type FancyNum has no destructor of its own; the move is blocked because DropStruct, the value the field is being moved out of, implements Drop. The same applies at any depth: moving o.inner.s is blocked by Inner's Drop impl if Inner has one, and by the outer type's if only the outer type has one - in each case the diagnostic names the type that implements Drop. Remove that impl and the identical program compiles - the traps section shows the same field move succeeding on a plain struct. One granularity point matters in the other direction: reading a Copy leaf through the fields is not a move out at all. The traps section reads a usize out of a field of a Drop struct twice and uses it both times, because a copy leaves the place filled.
How to read the diagnostic
The header names the outer type and the reason in one line: "cannot move out of type" - the type is named in backticks - "which implements the Drop trait". Read the named type first: it is the type with the destructor, which is not always the type of the thing you tried to move. The caret line then marks the move itself, labeled "cannot move out of here", and a second label states why the attempted operation is a move: the moved place "has type" some non-Copy type, "which does not implement the Copy trait". Where the move happens inside a pattern, the labels split - one marks the binding that receives the data ("data moved here") and the caret marks the matched value.
Unlike the borrow errors, an E0509 diagnostic usually carries a help: rewrite, and the helps are the fix menu in miniature. "consider borrowing here" adds a & to the move expression; in a pattern the same fix appears as "consider borrowing the pattern binding", which adds ref. "consider cloning the value" - sometimes with a note first pointing at the field type and suggesting it could implement Clone - turns the move into a clone. What no help offers is a swap: std::mem::take, Option::take, and ManuallyDrop never appear in the diagnostic, so when the field genuinely has to leave the value and stay gone, the fix the compiler does not print is the one to reach for.
The first failure: a struct field moved into a new binding
drop_struct is a DropStruct, which implements Drop, and the second line of main moves its fancy field into a new variable. The field type itself has no destructor - the block is on the outer type:
struct FancyNum {
num: usize,
}
struct DropStruct {
fancy: FancyNum,
}
impl Drop for DropStruct {
fn drop(&mut self) {}
}
fn main() {
let drop_struct = DropStruct { fancy: FancyNum { num: 5 } };
let fancy_field = drop_struct.fancy;
println!("Fancy: {}", fancy_field.num);
}
$ rustc --edition 2021 fail_struct_field.rs -o fail_struct_field
error[E0509]: cannot move out of type `DropStruct`, which implements the `Drop` trait
--> fail_struct_field.rs:15:23
|
15 | let fancy_field = drop_struct.fancy;
| ^^^^^^^^^^^^^^^^^
| |
| cannot move out of here
| move occurs because `drop_struct.fancy` has type `FancyNum`, which does not implement the `Copy` trait
|
note: if `FancyNum` implemented `Clone`, you could clone the value
--> fail_struct_field.rs:1:1
|
1 | struct FancyNum {
| ^^^^^^^^^^^^^^^ consider implementing `Clone` for this type
...
15 | let fancy_field = drop_struct.fancy;
| ----------------- you could clone this value
help: consider borrowing here
|
15 | let fancy_field = &drop_struct.fancy;
| +
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0509`.
The header names DropStruct, not FancyNum: the type that implements Drop is the value being moved out of. The note offers the Clone route - implement Clone on FancyNum and clone the field - and the help offers the borrow route. The borrow is the error codes index's own fix, and it is the one to take when the code only needs to read the field:
struct FancyNum {
num: usize,
}
struct DropStruct {
fancy: FancyNum,
}
impl Drop for DropStruct {
fn drop(&mut self) {}
}
fn main() {
let drop_struct = DropStruct { fancy: FancyNum { num: 5 } };
let ref fancy_field = drop_struct.fancy;
println!("Fancy: {}", fancy_field.num);
}
$ rustc --edition 2021 fix_ref_borrow.rs -o fix_ref_borrow
$ ./fix_ref_borrow
Fancy: 5
The output is Fancy: 5 - the field's number, read through the reference while drop_struct stays whole, and drops normally at the end of main. The ref keyword makes the binding a reference to the field in place; nothing leaves the struct.
The second failure: a field passed by value to a function
show takes its argument by value, so show(h.text) moves the text field out of h - and Holder implements Drop:
struct Holder {
text: String,
}
impl Drop for Holder {
fn drop(&mut self) {}
}
fn show(s: String) {
println!("show {}", s);
}
fn main() {
let h = Holder { text: String::from("hello") };
show(h.text);
}
$ rustc --edition 2021 fail_arg.rs -o fail_arg
error[E0509]: cannot move out of type `Holder`, which implements the `Drop` trait
--> fail_arg.rs:15:10
|
15 | show(h.text);
| ^^^^^^
| |
| cannot move out of here
| move occurs because `h.text` has type `String`, which does not implement the `Copy` trait
|
help: consider cloning the value if the performance cost is acceptable
|
15 | show(h.text.clone());
| ++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0509`.
The caret sits on the argument, because the call is where the move happens; the function never gets to run. The compiler's only help here is the clone, and the clone is the right fix when the function genuinely needs its own string and the holder keeps its field:
struct Holder {
text: String,
}
impl Drop for Holder {
fn drop(&mut self) {}
}
fn show(s: String) {
println!("show {}", s);
}
fn main() {
let h = Holder { text: String::from("hello") };
show(h.text.clone());
println!("still {}", h.text);
}
$ rustc --edition 2021 fix_clone.rs -o fix_clone
$ ./fix_clone
show hello
still hello
The output is show hello, from inside the function that received the clone, then still hello - the holder's own field, read after the call, because it never left. Where the function is yours to change, taking &str or &String instead removes the move entirely; where the signature is fixed, the clone is the price of keeping both values.
The third failure: a tuple struct field moved on its own
A tuple struct's field has no name, only a position, but the rule is the same: w.0 is a String inside a Wrapper that implements Drop:
struct Wrapper(String);
impl Drop for Wrapper {
fn drop(&mut self) {}
}
fn main() {
let w = Wrapper(String::from("hello"));
let s = w.0;
println!("{}", s);
}
$ rustc --edition 2021 fail_tuple.rs -o fail_tuple
error[E0509]: cannot move out of type `Wrapper`, which implements the `Drop` trait
--> fail_tuple.rs:9:13
|
9 | let s = w.0;
| ^^^
| |
| cannot move out of here
| move occurs because `w.0` has type `String`, which does not implement the `Copy` trait
|
help: consider borrowing here
|
9 | let s = &w.0;
| +
help: consider cloning the value if the performance cost is acceptable
|
9 | let s = w.0.clone();
| ++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0509`.
The diagnostic offers both printed fixes this time - borrow and clone - on the same expression. The borrow is the free one when the binding only needs to be read:
struct Wrapper(String);
impl Drop for Wrapper {
fn drop(&mut self) {}
}
fn main() {
let w = Wrapper(String::from("hello"));
let s = &w.0;
println!("{}", s);
}
$ rustc --edition 2021 fix_tuple_borrow.rs -o fix_tuple_borrow
$ ./fix_tuple_borrow
hello
The output is hello, the field read through the reference. A positional field is not a loophole around the rule, and it does not need one: every route that works for a named field - borrow, clone, swap - works at .0 unchanged.
The fourth failure: an enum payload bound by value in a match
Here the move happens inside a pattern. Matching on drop_enum by value and binding the Fancy payload to fancy_field would move the payload out of a DropEnum that implements Drop:
struct FancyNum {
num: usize,
}
enum DropEnum {
Fancy(FancyNum),
}
impl Drop for DropEnum {
fn drop(&mut self) {}
}
fn main() {
let drop_enum = DropEnum::Fancy(FancyNum { num: 10 });
match drop_enum {
DropEnum::Fancy(fancy_field) => println!("It was fancy-- {}!", fancy_field.num),
}
}
$ rustc --edition 2021 fail_enum.rs -o fail_enum
error[E0509]: cannot move out of type `DropEnum`, which implements the `Drop` trait
--> fail_enum.rs:15:11
|
15 | match drop_enum {
| ^^^^^^^^^ cannot move out of here
16 | DropEnum::Fancy(fancy_field) => println!("It was fancy-- {}!", fancy_field.num),
| ----------- data moved here because `fancy_field` has type `FancyNum`, which does not implement the `Copy` trait
|
help: consider borrowing the pattern binding
|
16 | DropEnum::Fancy(ref fancy_field) => println!("It was fancy-- {}!", fancy_field.num),
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0509`.
The labels split across the two lines: the caret is on the matched value, drop_enum itself, with "cannot move out of here", and the binding in the arm is marked as where the data moved. The help names the fix exactly - borrow the pattern binding:
struct FancyNum {
num: usize,
}
enum DropEnum {
Fancy(FancyNum),
}
impl Drop for DropEnum {
fn drop(&mut self) {}
}
fn main() {
let drop_enum = DropEnum::Fancy(FancyNum { num: 10 });
match drop_enum {
DropEnum::Fancy(ref fancy_field) => println!("It was fancy-- {}!", fancy_field.num),
}
}
$ rustc --edition 2021 fix_enum_ref.rs -o fix_enum_ref
$ ./fix_enum_ref
It was fancy-- 10!
The output is It was fancy-- 10! - the payload's number, read through the reference the ref binding created. This is the second fix the error codes index itself shows: the same ref technique, applied in a match arm instead of a let binding. Note what did not change: the match still owns and drops drop_enum after the arm runs; only the binding's mode changed.
The fifth failure: a struct destructured into its fields
A destructuring pattern is several moves at once. let Pair { name, count } = p; binds each field by value, and name is a String - a move out of a Pair that implements Drop:
struct Pair {
name: String,
count: u32,
}
impl Drop for Pair {
fn drop(&mut self) {}
}
fn main() {
let p = Pair { name: String::from("nails"), count: 3 };
let Pair { name, count } = p;
println!("{} {}", name, count);
}
$ rustc --edition 2021 fail_destructure.rs -o fail_destructure
error[E0509]: cannot move out of type `Pair`, which implements the `Drop` trait
--> fail_destructure.rs:12:32
|
12 | let Pair { name, count } = p;
| ---- ^ cannot move out of here
| |
| data moved here because `name` has type `String`, which does not implement the `Copy` trait
|
help: consider borrowing the pattern binding
|
12 | let Pair { ref name, count } = p;
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0509`.
One error covers the pattern, and its labels show why: the caret marks the whole value p as the place that cannot be moved out of, while the name binding is marked as where the data moved. The count binding draws no label - a u32 is Copy, so binding it copies rather than moves, and a copy out is not blocked. The fix borrows only the field that would move:
struct Pair {
name: String,
count: u32,
}
impl Drop for Pair {
fn drop(&mut self) {}
}
fn main() {
let p = Pair { name: String::from("nails"), count: 3 };
let Pair { ref name, count } = p;
println!("{} {}", name, count);
}
$ rustc --edition 2021 fix_destructure_ref.rs -o fix_destructure_ref
$ ./fix_destructure_ref
nails 3
The output is nails 3 - the name read through its reference, the count copied outright. Destructuring a Drop type is not banned; each binding is judged on its own, and ref on the non-Copy bindings is enough. Where every field is Copy, the same pattern compiles with no ref at all.
When the field really has to leave: swap it out
Borrowing and cloning both leave the original field in place. Sometimes the field's value has to end up owned by the caller and gone from the struct - and then the legal route is to leave a replacement behind, through a mutable borrow of the field. std::mem::take leaves the field type's Default:
struct Holder {
text: String,
}
impl Drop for Holder {
fn drop(&mut self) {}
}
fn main() {
let mut h = Holder { text: String::from("hello") };
let taken = std::mem::take(&mut h.text);
println!("taken {} now [{}]", taken, h.text);
}
$ rustc --edition 2021 fix_take_default.rs -o fix_take_default
$ ./fix_take_default
taken hello now []
The output is taken hello now [] - the caller owns the original string, and the field holds the empty default string, so h is whole when it drops. The requirement is in the signature: the field's type must implement Default, as String does. The traps section shows what happens when it does not.
An Option field has the specialized form of the same swap built in - take leaves None, and needs no Default impl on the payload type:
struct Holder {
text: Option<String>,
}
impl Drop for Holder {
fn drop(&mut self) {}
}
fn main() {
let mut h = Holder { text: Some(String::from("hello")) };
let taken = h.text.take();
println!("taken {:?} now {:?}", taken, h.text);
}
$ rustc --edition 2021 fix_option_take.rs -o fix_option_take
$ ./fix_option_take
taken Some("hello") now None
The output is taken Some("hello") now None. Holding a field in an Option purely so it can be taken out later is a common shape in builder and state-machine code for exactly this reason: the option is the documented hole the value is allowed to leave through.
The last resort is to switch the destructor off. ManuallyDrop wraps the value and suppresses the automatic drop, and through the wrapper the field can be taken with the same std::mem::take:
use std::mem::ManuallyDrop;
struct Holder {
text: String,
}
impl Drop for Holder {
fn drop(&mut self) {}
}
fn main() {
let mut h = ManuallyDrop::new(Holder { text: String::from("hello") });
let taken = std::mem::take(&mut h.text);
println!("taken {}", taken);
}
$ rustc --edition 2021 fix_manually_drop.rs -o fix_manually_drop
$ ./fix_manually_drop
taken hello
The output is taken hello. The trade is real: nothing calls Holder's drop automatically anymore, so if that destructor did work - closing, flushing, unregistering - the code now owes that call explicitly. Reach for ManuallyDrop when the value is being taken apart as the last step of its life, not as a routine way around the error.
The fixes in reach order
- The code only needs to read the field: borrow it -
&value.fieldin an expression,refin a let binding or a match arm. No copy, no change to the value, and the destructor runs as written. - The code needs its own copy and the field type implements
Clone: clone the field, as the diagnostic's help proposes. The original stays in place; the cost is the copy. - The field has to leave and its type implements
Default:std::mem::take(&mut value.field)- the caller gets the value, the field gets the default, the struct stays whole. - The field has to leave and can be replaced with a specific value:
std::mem::replace(&mut value.field, replacement)- the same swap with your replacement instead of the default. - The field is an
Option: design for the take -value.field.take()leavesNoneand needs no trait beyond the option itself. - The value is being dismantled at the end of its life and none of the above fits: wrap it in
ManuallyDropand take the fields through the wrapper, owing the destructor call yourself. - The
Dropimpl does no work with the fields - an empty marker impl, or one that only logs: reconsider whether the type needsDropat all. Removing a needless impl makes every partial move in this guide legal at once; keeping a needed one and working around it is the rest of this list.
Traps
- Moving the whole value is not E0509 at all. The block is on moving out of the value; the value itself, destructor and fields together, moves freely:
struct Holder {
text: String,
}
impl Drop for Holder {
fn drop(&mut self) {}
}
fn main() {
let h = Holder { text: String::from("hello") };
let h2 = h;
println!("{}", h2.text);
}
$ rustc --edition 2021 probe_whole_move.rs -o probe_whole_move
$ ./probe_whole_move
hello
hello - the holder's field, read after the whole holder moved to h2. If your code can be restructured to move the unit and work on it at its new home, there is no error to fix.
- Moving a field out of a type without Drop is not E0509 either - it is a legal partial move. This is the second family's field move with the Drop impl removed:struct Plain {
text: String,
}
fn main() {
let p = Plain { text: String::from("hello") };
let s = p.text;
println!("{}", s);
}
$ rustc --edition 2021 probe_nodrop_field.rs -o probe_nodrop_field
$ ./probe_nodrop_field
hello
The output is hello. The struct is left partially moved - p itself can no longer be used as a whole - but no destructor exists that could observe the missing field, so the move is allowed. Adding a Drop impl to a type is therefore a breaking change for any caller that moves its fields out, even when the impl body is empty, as every family in this guide shows: each failing program's drop method does nothing.
- Reading a Copy leaf through the fields is not a move out. Here a usize is read out of a field of a Drop struct, twice:
struct FancyNum {
num: usize,
}
struct DropStruct {
fancy: FancyNum,
}
impl Drop for DropStruct {
fn drop(&mut self) {}
}
fn main() {
let drop_struct = DropStruct { fancy: FancyNum { num: 5 } };
let n = drop_struct.fancy.num;
println!("{} {}", n, drop_struct.fancy.num);
}
$ rustc --edition 2021 probe_copy_field.rs -o probe_copy_field
$ ./probe_copy_field
5 5
The output is 5 5. A copy leaves the place filled, so there is nothing for the destructor to miss. Before restructuring around E0509, check the moved place's type in the diagnostic's second label: if the value you need is a Copy leaf one level further in - a number inside the field struct - reading it directly may already be legal.
- Moving a field out through a reference is E0507, not E0509. Here the holder is reached through a shared reference r, and the same field move fails earlier, at the reference:
struct Holder {
text: String,
}
impl Drop for Holder {
fn drop(&mut self) {}
}
fn main() {
let h = Holder { text: String::from("hello") };
let r = &h;
let s = r.text;
println!("{}", s);
}
$ rustc --edition 2021 probe_e0507_ref.rs -o probe_e0507_ref
error[E0507]: cannot move out of `r.text` which is behind a shared reference
--> probe_e0507_ref.rs:12:13
|
12 | let s = r.text;
| ^^^^^^ move occurs because `r.text` has type `String`, which does not implement the `Copy` trait
|
help: consider borrowing here
|
12 | let s = &r.text;
| +
help: consider cloning the value if the performance cost is acceptable
|
12 | let s = r.text.clone();
| ++++++++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0507`.
The header says the field "is behind a shared reference" - the move is blocked because the value is borrowed, before the Drop question is ever reached. The error codes index defines E0507 as a borrowed value being moved out. The full separation from the other move errors is in the E0507 guide.
- Using the value after the whole value moved is E0382, not a move-out error at all. The whole-holder move from the first trap compiles; touching the old binding afterward does not:
struct Holder {
text: String,
}
impl Drop for Holder {
fn drop(&mut self) {}
}
fn main() {
let h = Holder { text: String::from("hello") };
let h2 = h;
println!("{}", h.text);
println!("{}", h2.text);
}
$ rustc --edition 2021 probe_e0382.rs -o probe_e0382
error[E0382]: borrow of moved value: `h`
--> probe_e0382.rs:12:20
|
10 | let h = Holder { text: String::from("hello") };
| - move occurs because `h` has type `Holder`, which does not implement the `Copy` trait
11 | let h2 = h;
| - value moved here
12 | println!("{}", h.text);
| ^^^^^^ value borrowed here after move
|
note: if `Holder` implemented `Clone`, you could clone the value
--> probe_e0382.rs:1:1
|
1 | struct Holder {
| ^^^^^^^^^^^^^ consider implementing `Clone` for this type
...
11 | let h2 = h;
| - you could clone this value
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0382`.
The header reads "borrow of moved value" - the move already happened, legally, and the error is at the later use. The error codes index defines E0382 as a value used after being moved. The fixes live in the E0382 guide. - A field you cannot name is E0616, not a move problem. This program never tries to move anything - it reads a field that is private to another module:
mod inner {
pub struct Vault {
secret: String,
}
impl Vault {
pub fn new() -> Vault {
Vault { secret: String::from("s3cr3t") }
}
}
}
fn main() {
let v = inner::Vault::new();
println!("{}", v.secret);
}
$ rustc --edition 2021 probe_e0616.rs -o probe_e0616
error[E0616]: field `secret` of struct `Vault` is private
--> probe_e0616.rs:15:22
|
15 | println!("{}", v.secret);
| ^^^^^^ private field
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0616`.
The header says the field "is private", full stop - no move, no Drop, no help: rewrite. When a field access fails, read the header before reaching for this guide's fixes: a visibility error is fixed with pub or an accessor method, and borrowing a private field fails the same way moving it does. The error codes index defines E0616 as an attempt to access a private field.
- std::mem::take on a field whose type has no Default is E0277, not E0509. The swap fix has a trait bound, and a field type without Default fails it at the call:
#[derive(Debug)]
struct Token {
id: u32,
}
struct Holder {
token: Token,
}
impl Drop for Holder {
fn drop(&mut self) {}
}
fn main() {
let mut h = Holder { token: Token { id: 7 } };
let taken = std::mem::take(&mut h.token);
println!("taken {:?}", taken);
}
$ rustc --edition 2021 probe_take_no_default.rs -o probe_take_no_default
error[E0277]: the trait bound `Token: Default` is not satisfied
--> probe_take_no_default.rs:16:32
|
16 | let taken = std::mem::take(&mut h.token);
| -------------- ^^^^^^^^^^^^ the trait `Default` is not implemented for `Token`
| |
| required by a bound introduced by this call
|
note: required by a bound in `std::mem::take`
--> /home/droopy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/mem/mod.rs:894:22
|
894 | pub const fn take<T: [const] Default>(dest: &mut T) -> T {
| ^^^^^^^^^^^^^^^ required by this bound in `take`
help: consider annotating `Token` with `#[derive(Default)]`
|
2 + #[derive(Default)]
3 | struct Token {
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0277`.
The header is a trait-bound failure - "Token: Default is not satisfied" - reported against the take call, not against the struct. The routes from here are the ones the bound itself suggests: derive or implement Default for the field type, use std::mem::replace with an explicit replacement, or fall back to borrowing or cloning the field.
Decision summary
- The header says "cannot move out of type" a named type "which implements the
Droptrait", with the caret on the move and a label naming the moved place's non-Copytype: a field or payload is being moved out of aDropvalue - borrow it, clone it, or swap a replacement in. - The header names a type you did not touch: that is the outer type with the destructor - the field's own type is in the second label, and it is the outer type's
Dropimpl that blocks the move. - The move is inside a pattern - a match arm or a destructuring let: the labels split between the matched value and the binding, and the fix is
refon each non-Copybinding. - No error where a field leaves a struct: the type has no
Dropimpl, and the move is a legal partial move - or the value taken was aCopyleaf, which copies rather than moves. - No error where the whole value changes hands: moving the unit is always legal; only moving out of it is blocked.
- The header instead says a field "is behind a shared reference" (E0507): the move is blocked by the borrow first - the value would have to be owned, not referenced, before the
Dropquestion arises. - The header instead says "borrow of moved value" (E0382): the move already succeeded and the error is the later use of the old binding.
- The header instead says a field "is private" (E0616): a visibility error - fix it with
pubor an accessor, not with a move fix. - The header instead says a trait bound "is not satisfied" (E0277) at a
mem::takecall: the swap fix needsDefaulton the field type - supply it, usereplace, or borrow or clone instead.
Checklist
- The header read for the named type: that is the type whose
Dropimpl blocks the move - confirm it is the outer value's type, not the field's. - The moved place's type read from the second label: non-
Copyis what makes the operation a move; aCopyleaf at the same spot would compile. - The route chosen by what the code needs: read-only access borrows, an independent copy clones, a genuine extraction swaps with
take,replace, orOption::take. - Pattern moves checked binding by binding:
refon each non-Copybinding,Copybindings left alone. - Any
mem::takechecked against the field type'sDefaultimpl before it is written - andreplacekept in mind when there is none. ManuallyDropused only where the value is being dismantled, with the owed destructor call written explicitly.- The
Dropimpl itself re-read: if it does no work with the fields, removing it is the cheapest fix of all. - The fixed program run, and its output checked against what the failing program was trying to compute.
Related guides on this site
- E0505 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
- E0503 Cannot Use Fix
- E0425 Cannot Find Value Fix
Sources
- Rust, Error codes index: E0509 - the definition of the error: an attempt was made to move out of a value whose type implements the
Droptrait, because a struct cannot be dropped if one or more of its fields have been moved; its fixes create a reference to the fields using therefkeyword, in a let binding and in the arms of a match expression. - The Rust Programming Language, What Is Ownership? - the move and
Copyrules the diagnostic's labels rest on: a value of a non-Copytype is moved rather than copied on assignment or binding, whileCopytypes are copied and leave the original usable. - The Rust Reference, Destructors - the destructor rules: a type's
Dropimplementation runs when a value of that type goes out of scope, after which its fields are dropped recursively; after a partial move, only the remaining fields are dropped. - Rust, Error codes index: E0507 - the neighboring move error: a borrowed value was moved out; the distinction from E0509 is that the block is the borrow, not the destructor.
- Rust, Error codes index: E0382 - the neighboring use error: a value was used after it was moved; the distinction from E0509 is that the move itself was legal and complete, and the error is at the later use.
- Rust, Error codes index: E0616 - the neighboring access error: a field of a struct was accessed but is private; the distinction from E0509 is that no move is involved - the field cannot be named from that code at all.
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.