E0120 Drop on Trait Object or Reference Fix
Written December 3, 2026 · Tested with Rust 1.99.1 · 21 min read
- The rule underneath: Drop belongs to a local nominal type
- How to read the diagnostic
- The first failure: the index reference example
- The index trait example, and its outdated syntax
Written December 3, 2026
Tested with Rust 1.99.1
What this guide is
Error E0120 means the Drop trait was implemented for something that is not a struct, enum, or union defined in the current crate - in the index's words, Drop was implemented on a trait object or reference, which is not allowed, and only structs, enums, and unions can implement Drop. The error codes index gives two erroneous examples, one implementing Drop for a trait and one implementing it for a mutable reference, and its workaround is a wrapper struct that holds the value and implements Drop itself.
In the shapes probed in this guide, the index reference example verbatim drew exactly one E0120 and no other code. The index trait example as printed drew a different code instead - one E0782 and no E0120 - because it names the trait bare, without dyn, and a bare trait name is no longer a type on this compiler; the same impl with dyn written in drew exactly one E0120, so the trait-object shape is an E0120 shape once its syntax is current. E0120 fired on its own in the probed shapes for a shared reference, a mutable reference with a named lifetime, a trait object, and a trait object with an added bound. The count follows the impl blocks, in the probed shapes: two offending reference blocks drew two reports. Other targets drew E0120 with a partner code in the probed shapes: a primitive, a foreign nominal type, a slice, an array, a tuple, the str type, and a function pointer each drew the orphan-rule code E0117 alongside it, because Drop itself is a foreign trait; a bare generic parameter drew E0210 alongside it; and a Box drew the conflicting-implementation code E0119 alongside it, because Box already implements Drop. At the boundary, Drop implemented for a local struct, a local enum, a local union, a generic local struct, and an alias to a local struct compiles and runs, and a local trait implemented for a reference or for a trait object compiles and runs - the restriction is specific to Drop, not to trait implementations in general. This guide shows the E0120 shapes, the outdated syntax in the first index example, the partnered shapes, the boundary, and the repairs.
Build every program in this guide with rustc --edition 2021. In the compiler transcripts, the file names are the bare names shown on the command line.
The rule underneath: Drop belongs to a local nominal type
Drop is the trait the compiler calls when a value goes out of scope, and it is special among traits: an implementation of Drop is part of a type's own definition, so it may only be written where the type itself is defined, and only for a type that has a definition of that kind - a struct, enum, or union in the current crate. A reference is not such a type: &Concrete and &mut Concrete borrow a value owned elsewhere, and the borrowed value's own type already decides what runs at the end of its life. A trait object is not such a type either: dyn MyTrait names a way of calling methods, not a concrete type whose storage and lifetime the compiler manages. That is the mismatch E0120 reports, and neither spelling change cures it in the probed shapes - naming the reference's lifetime drew the same single E0120, and adding a Send bound to the trait object drew the same single E0120.
Two different questions are easy to confuse here, and the probed shapes separate them. May this trait be implemented for this target at all, under the orphan rules? Drop comes from the standard library, so for a target with no local type in it - a primitive, a foreign type, a slice - that question fails too, and E0117 is reported alongside E0120. Is the target the kind of type that may carry Drop? That question is E0120's alone, and for a reference to a local type or a trait object over a local trait it is the only question that fails, so E0120 stands alone. Read both codes when both appear: fixing the orphan problem by itself does not make a reference a struct, and the E0120 remains until the target is a local nominal type.
The honest repair follows from the rule. Give the cleanup to a type of your own: define a struct that owns or holds the value - generically, as a reference to a trait object, as an owned field, or as a boxed trait object - and implement Drop on the struct. The struct is local and nominal, so both questions pass, and the wrapper's drop runs when the wrapper goes out of scope.
How to read the diagnostic
The E0120 header is fixed wording: "the Drop trait may only be implemented for local structs, enums, and unions". The single span covers the target type alone - in the probed shapes, &'_ mut Concrete underlined on the impl line - and labels it "must be a struct, enum, or union in the current crate". There is no help line and no note in the clean shapes: the diagnostic names the requirement, and the wrapper workaround comes from the index, not from the report.
The count follows the impl blocks, in the probed shapes. Two blocks, one for a shared reference and one for a mutable reference to the same struct, drew exactly two E0120 reports, one per block, ending with "aborting due to 2 previous errors". Fix every offending block in the output before recompiling; a second bad block is already reported, not hidden behind the first.
The first failure: the index reference example
The index's second erroneous example, verbatim - Drop implemented for a mutable reference to a local struct:
#![allow(unused)]
fn main() {
struct Concrete {}
impl Drop for &'_ mut Concrete {
fn drop(&mut self) {}
}
}
error[E0120]: the `Drop` trait may only be implemented for local structs, enums, and unions
--> fail_index.rs:5:15
|
5 | impl Drop for &'_ mut Concrete {
| ^^^^^^^^^^^^^^^^ must be a struct, enum, or union in the current crate
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0120`.
Exactly one report. The target's pointee is local - Concrete is defined in the same program - and that does not help: the impl is written on the reference, not on Concrete, and a reference is not a struct, enum, or union. The shared form is the same shape: Drop implemented for &Concrete drew exactly one E0120 in the probed shape, in the file fail_ref_shared.rs, and spelling the lifetime out - impl<'a> Drop for &'a mut Concrete - drew exactly one E0120 in the probed shape, in the file fail_ref_named_lifetime.rs. The lifetime is not the defect; the reference is.
The index trait example, and its outdated syntax
The index's first erroneous example, verbatim - Drop implemented for a trait named bare:
#![allow(unused)]
fn main() {
trait MyTrait {}
impl Drop for MyTrait {
fn drop(&mut self) {}
}
}
error[E0782]: expected a type, found a trait
--> fail_index_trait.rs:5:15
|
5 | impl Drop for MyTrait {
| ^^^^^^^
|
help: you can add the `dyn` keyword if you want a trait object
|
5 | impl Drop for dyn MyTrait {
| +++
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0782`.
No E0120 appears. On this compiler a bare trait name in type position is not a type at all, and that older defect is reported first, as E0782, with a help line offering the dyn keyword. The E0782 belongs to the bare-name syntax, not to Drop: a plain function taking the trait bare, with no Drop impl anywhere in the program, drew exactly one E0782 on its own in the probed shape, in the file sep_e0782_alone.rs. And with the syntax made current, the shape underneath is exactly the one the index means - Drop implemented for dyn MyTrait drew exactly one E0120 and no other code:
error[E0120]: the `Drop` trait may only be implemented for local structs, enums, and unions
--> fail_dyn_trait.rs:2:15
|
2 | impl Drop for dyn MyTrait {
| ^^^^^^^^^^^ must be a struct, enum, or union in the current crate
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0120`.
Adding a bound does not cure it, in the probed shapes: Drop implemented for dyn MyTrait + Send drew exactly one E0120, in the file fail_dyn_trait_bounds.rs. A trait object stays a trait object however it is bounded, and a trait object cannot carry Drop.
The shapes where a second code rides along
Aim the same impl at a target with no local type in it, and the orphan rule fails beside E0120. Drop implemented for u32 drew one E0117 and one E0120 together in the probed shape:
error[E0117]: only traits defined in the current crate can be implemented for primitive types
--> fail_primitive.rs:1:1
|
1 | impl Drop for u32 {
| ^^^^^^^^^^^^^^---
| |
| `u32` is not defined in the current crate
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
error[E0120]: the `Drop` trait may only be implemented for local structs, enums, and unions
--> fail_primitive.rs:1:15
|
1 | impl Drop for u32 {
| ^^^ must be a struct, enum, or union in the current crate
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0117, E0120.
For more information about an error, try `rustc --explain E0117`.
The E0117 comes first and is about the trait's origin: Drop is defined in the standard library, and neither it nor u32 is local. The E0120 follows and is about the target's kind: a primitive is not a struct, enum, or union of this crate. The whole no-local-type family landed on the same pair in the probed shapes: a foreign nominal type - Vec and String - a slice, an array, a tuple, the str type, and a function pointer each drew exactly one E0117 plus exactly one E0120 - the full transcripts are the fail shapes under each target's name, in the files fail_foreign_vec.rs, fail_foreign_string.rs, fail_slice.rs, fail_array.rs, fail_tuple.rs, fail_str.rs, and fail_fn_pointer.rs. The Vec shape, for the record:
error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
--> fail_foreign_vec.rs:1:1
|
1 | impl Drop for Vec<u8> {
| ^^^^^^^^^^^^^^-------
| |
| `Vec` is not defined in the current crate
|
= note: impl doesn't have any local type before any uncovered type parameters
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead
error[E0120]: the `Drop` trait may only be implemented for local structs, enums, and unions
--> fail_foreign_vec.rs:1:15
|
1 | impl Drop for Vec<u8> {
| ^^^^^^^ must be a struct, enum, or union in the current crate
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0117, E0120.
For more information about an error, try `rustc --explain E0117`.
A bare generic parameter draws a different partner. Drop implemented for T drew one E0210 plus one E0120 in the probed shape - E0210 because a foreign trait may not be implemented for a bare parameter, and E0120 because a parameter is not a local nominal type:
error[E0210]: type parameter `T` must be used as an argument to some local type (e.g., `MyStruct<T>`)
--> fail_generic_param.rs:1:6
|
1 | impl<T> Drop for T {
| ^ uncovered type parameter
|
= note: implementing a foreign trait is only possible if at least one of the types for which it is implemented is local
= note: only traits defined in the current crate can be implemented for a type parameter
error[E0120]: the `Drop` trait may only be implemented for local structs, enums, and unions
--> fail_generic_param.rs:1:18
|
1 | impl<T> Drop for T {
| ^ must be a struct, enum, or union in the current crate
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0120, E0210.
For more information about an error, try `rustc --explain E0120`.
A Box draws a third partner. Drop implemented for Box<dyn MyTrait> drew one E0119 plus one E0120 in the probed shape - E0119 because Box already implements Drop in the standard library, so the new impl conflicts with an existing one before its target is even judged:
error[E0119]: conflicting implementations of trait `Drop` for type `Box<(dyn MyTrait + 'static)>`
--> fail_box_dyn.rs:2:1
|
2 | impl Drop for Box<dyn MyTrait> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: conflicting implementation in crate `alloc`:
- impl<T, A> Drop for Box<T, A>
where A: Allocator, T: ?Sized;
error[E0120]: the `Drop` trait may only be implemented for local structs, enums, and unions
--> fail_box_dyn.rs:2:15
|
2 | impl Drop for Box<dyn MyTrait> {
| ^^^^^^^^^^^^^^^^ must be a struct, enum, or union in the current crate
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0119, E0120.
For more information about an error, try `rustc --explain E0119`.
Box<Local>, a box of a local struct, drew the same E0119 plus E0120 pair in the probed shape, in the file fail_box_local.rs. Wrapping in a Box is therefore not a repair by itself - the box is the standard library's type, with its own Drop already written. The repair below puts the box inside a struct of your own instead.
Other errors in the same program are reported alongside E0120 as well. A program whose offending impl shared the file with an unresolved name in the entry point drew E0425 and E0120 together in the probed shape, ending with "aborting due to 2 previous errors":
error[E0425]: cannot find value `missing_name` in this scope
--> ride_unresolved.rs:5:28
|
5 | fn main() { println!("{}", missing_name); }
| ^^^^^^^^^^^^ not found in this scope
error[E0120]: the `Drop` trait may only be implemented for local structs, enums, and unions
--> ride_unresolved.rs:2:15
|
2 | impl Drop for &Concrete {
| ^^^^^^^^^ must be a struct, enum, or union in the current crate
error: aborting due to 2 previous errors
Some errors have detailed explanations: E0120, E0425.
For more information about an error, try `rustc --explain E0120`.
Fixing the impl clears the E0120 in that shape and leaves the E0425 standing - the unresolved name is a separate problem that happens to share the output.
The boundary: targets Drop accepts
The line is whether the target is a struct, enum, or union defined in this crate, and on the other side of it the same impl shape is ordinary code. Drop implemented for a local struct compiled and ran in the probed shape, its drop printing dropped 3 as the value went out of scope at the end of the entry point:
struct Local { v: u32 }
impl Drop for Local {
fn drop(&mut self) { println!("dropped {}", self.v); }
}
fn main() { let _x = Local { v: 3 }; }
dropped 3
A local enum and a local union take Drop the same way: in the probed shapes the enum's drop printed dropped enum and the union's printed dropped union. A generic local struct is still nominal - Drop implemented for Wrap<T>, a struct the program defines, compiled and ran in the probed shape, printing dropped wrap. Genericity is not the defect; the generic-parameter shape above fails because its target is the parameter itself, not a generic type built around one.
An alias to a local struct takes Drop too, in the probed shape: an alias naming a local struct, with the impl written on the alias, compiled and ran, printing dropped alias - the alias looks through to a local nominal type, and that is enough.
And the restriction is specific to Drop. A trait defined in the program, implemented for a shared reference to a local struct, compiled and ran in the probed shape, printing local trait for ref; the same local trait implemented for a trait object compiled and ran in the probed shape, printing local trait for dyn. References and trait objects accept ordinary trait implementations - what they cannot accept is Drop, because Drop is part of a type's own definition rather than a behavior added to it.
Related guides
For a trait implementation where neither the trait nor the type is local, the neighboring code is E0117. For a foreign trait implemented for a bare type parameter, the neighboring code is E0210. For two implementations of the same trait for the same type, the neighboring code is E0119. For a trait named bare where a type is expected, the neighboring code is E0782. For an unresolved name used in the same program, the neighboring code is E0425.
The repairs
Every repair below starts from the same observation: the impl target is not a local struct, enum, or union. Give the value a struct of your own that holds it, and implement Drop on the struct - the wrapper goes out of scope at the same point the value would have, so the cleanup still runs where it was meant to.
Wrap the value generically. This is the index's own first workaround: a wrapper struct with a generic field, a trait bound on the parameter, and Drop implemented on the wrapper. It compiled and ran in the probed shape, printing dropped wrapper when the wrapper went out of scope:
trait MyTrait {}
struct S;
impl MyTrait for S {}
struct MyWrapper<T: MyTrait> { foo: T }
impl<T: MyTrait> Drop for MyWrapper<T> {
fn drop(&mut self) { println!("dropped wrapper"); }
}
fn main() { let _w = MyWrapper { foo: S }; }
dropped wrapper
Let the wrapper hold a trait object. This is the index's second workaround, for the trait-object shape: the wrapper holds a reference to the trait object, and Drop is implemented on the wrapper. It compiled and ran in the probed shape, printing dropped object wrapper:
trait MyTrait {}
struct S;
impl MyTrait for S {}
struct MyWrapper<'a> { foo: &'a dyn MyTrait }
impl<'a> Drop for MyWrapper<'a> {
fn drop(&mut self) { println!("dropped object wrapper"); }
}
fn main() { let s = S; let _w = MyWrapper { foo: &s }; }
dropped object wrapper
Wrap the concrete value you actually have. Most real code holds one concrete type, and the direct form of the repair is a struct that owns it: a wrapper owning the struct the reference used to borrow compiled and ran in the probed shape, its drop reading the wrapped value back out and printing dropped owned 42:
struct Concrete { v: u32 }
struct Owned { inner: Concrete }
impl Drop for Owned {
fn drop(&mut self) { println!("dropped owned {}", self.inner.v); }
}
fn main() { let _w = Owned { inner: Concrete { v: 42 } }; }
dropped owned 42
Where the value is already boxed, put the box inside your struct rather than implementing Drop for the box: a struct holding a boxed trait object, with Drop on the struct, compiled and ran in the probed shape, printing dropped box wrapper - the shape that drew E0119 plus E0120 when the impl was written on the Box itself.
Fixes in reach order
First, read every code on the report before choosing a repair. E0120 says the target is not a local struct, enum, or union. When E0117 stands beside it, the target also has no local type in it; when E0210 stands beside it, the target is a bare parameter; when E0119 stands beside it, the target already has a Drop implementation of its own. Each partner confirms the same repair from a different side.
Second, if the target is a reference, stop implementing Drop for the reference. Implement it for the pointee if the pointee is a local nominal type and you own its definition, or wrap the value in a struct you own and implement Drop there.
Third, if the target is a trait object, use the index's wrapper: a struct that holds the object - by reference or boxed - with Drop on the struct. Adding dyn fixes the syntax of the trait example, not the error; the dyn form drew the same E0120.
Fourth, do not reach for a Box as the wrapper. A Box is a foreign type that already implements Drop, and an impl written on it drew E0119 alongside E0120 in the probed shapes. A struct holding the box is the wrapper that works.
Fifth, if the output also carries an E0425, separate it before judging the program: an unresolved name elsewhere in the program is a defect of its own, and it survives the Drop fix until it is fixed on its own terms.
Decision summary
E0120 fired on its own in the probed shapes for the index reference example (exactly one E0120), for a shared reference and for a mutable reference with a named lifetime (exactly one E0120 each), for a trait object written with dyn and for a trait object with an added Send bound (exactly one E0120 each), and alongside an unresolved name as an E0425 plus E0120 pair. The count followed the impl blocks in the probed shapes: two reference blocks drew two E0120 reports. It fired with a partner in the probed shapes for a primitive, a foreign nominal type, a slice, an array, a tuple, the str type, and a function pointer (one E0117 plus one E0120 each), for a bare generic parameter (one E0210 plus one E0120), and for a boxed trait object and a boxed local struct (one E0119 plus one E0120 each). The index trait example as printed drew no E0120 in the probed shape - it drew exactly one E0782, which a bare trait name in an unrelated function signature also drew on its own. It did not fire, in the probed shapes, for Drop implemented for a local struct (compiled and ran, printing dropped 3), a local enum (printing dropped enum), a local union (printing dropped union), a generic local struct (printing dropped wrap), or an alias to a local struct (printing dropped alias), or for a local trait implemented for a reference (printing local trait for ref) or a trait object (printing local trait for dyn). The repairs that compiled and ran in the probed shapes were the generic wrapper (printing dropped wrapper), the wrapper holding a trait object reference (printing dropped object wrapper), the wrapper owning a concrete value (printing dropped owned 42), and the wrapper holding a boxed trait object (printing dropped box wrapper).
Checklist
- The target is the test, in the probed shapes:
Dropmay only be implemented for a struct, enum, or union defined in this crate - a reference, a trait object, a primitive, and a foreign type are none of those. - A local pointee does not cure a reference target, in the probed shapes:
Dropfor a reference to a local struct drew E0120 however the lifetime was spelled. - A bound does not cure a trait-object target either, in the probed shapes: a trait object with a
Sendbound drew the same E0120. - The count is per impl block, in the probed shapes: two offending reference blocks drew two reports.
- A partner code names the second failure, in the probed shapes: no local type in the target is E0117, a bare parameter is E0210, and an already-implemented target is E0119 - the E0120 remains until the target is a local nominal type.
- On the other side of the line there is no error, in the probed shapes: a local struct, enum, union, generic struct, or local-struct alias takes its
Dropimpl and runs, and ordinary local traits can still be implemented for references and trait objects.
Sources
- Rust error codes index, E0120 -
Dropwas implemented on a trait object or reference, which is not allowed; only structs, enums, and unions can implementDrop; the erroneous examples implementDropfor a trait and for a mutable reference, and the workarounds are wrapper structs holding the value or the trait object. - Rust error codes index, E0117 - the orphan rule allows only local traits or local types in a trait implementation; the neighboring code drawn alongside E0120 in the probed shapes by a primitive, a foreign type, a slice, an array, a tuple, the str type, and a function pointer.
- Rust error codes index, E0210 - a type parameter must be used as an argument to a local type in a foreign trait implementation; the neighboring code drawn alongside E0120 in the probed shape by a bare generic parameter.
- Rust error codes index, E0119 - conflicting implementations of a trait for a type; the neighboring code drawn alongside E0120 in the probed shapes by a boxed trait object and a boxed local struct, which already implement
Drop. - Rust error codes index, E0782 - trait objects must include the
dynkeyword; the code drawn in the probed shape by the index trait example as printed, and by a bare trait name in an unrelated function signature. - Rust error codes index, E0425 - an unresolved name was used; the neighboring code drawn in the probed shape alongside E0120 by a program that also used an unresolved name.
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.