Rust

E0040 Explicit Destructor Call Fix

A destructor is not a method you call: the compiler runs it when a value goes out of scope, and calling it yourself is E0040. This guide shows both forbidden call forms and the three allowed ways to control when - or whether - a value is dropped.

Written November 28, 2026 · Tested with Rust 1.99.1 · 12 min read

By the end of this guide
  • The rule underneath: the compiler owns destructor calls
  • The first failure: calling the method on the value
  • The second failure: calling the trait function explicitly
  • First fix: let the scope end

Written November 28, 2026

Tested with Rust 1.99.1

What this guide is

Error E0040 is a manual destructor call. The error codes index states it in one line: it is not allowed to manually call destructors in Rust. The call looks ordinary - a method call on a value whose type implements Drop - but the compiler refuses it in every form, whether it is written as a method call, g.drop(), or as an explicit trait call, Drop::drop(&mut g). The reason is the other drop: a value's destructor runs automatically when the value goes out of scope, so a manual call would leave the value in place to be dropped a second time at scope end, and a destructor that frees a resource twice is unsound. Rust therefore reserves destructor calls to the compiler, and to one unsafe library function covered below.

This guide reproduces E0040 in both call forms with the same small type, then gives the allowed routes in the order most code wants them. Let the scope end where the value should die. Move the value into std::mem::drop to drop it at an exact point before scope end - that function is not the destructor method, it works by taking ownership so the value's own scope ends inside the call. And, for storage you manage manually, run the destructor in place with std::ptr::drop_in_place. A final section covers the opposite request - stopping a drop from happening at all - and the error you get for using a value after std::mem::drop has taken it.

The rule underneath: the compiler owns destructor calls

The Rust Reference's destructors chapter defines a type's destructor as calling <T as Drop>::drop if the type implements Drop, then recursively dropping its fields - and that destructor runs when an initialized variable or temporary goes out of scope. Because the compiler inserts that call itself, it also forbids yours: after a manual drop(&mut self) returned, the value would still be initialized, still owned, and still due its automatic drop. Rather than track whether a destructor already ran, Rust bans the manual call outright - there is no annotation, edition, or unsafe block that makes g.drop() on an owned value legal. The one sanctioned manual route is std::ptr::drop_in_place, which the Reference names for exactly the case where a destructor must be run manually, such as implementing your own smart pointer - and it is only sound where the value will not be dropped again, which is why the example below wraps the value in ManuallyDrop first.

The first failure: calling the method on the value

Rust
struct Guard {
    name: &'static str,
}

impl Drop for Guard {
    fn drop(&mut self) {
        println!("dropping {}", self.name);
    }
}

fn main() {
    let mut g = Guard { name: "file lock" };
    g.drop();
    println!("still in main");
}
Terminal
$ rustc --edition 2021 p1_fail_method.rs -o p1_fail_method.bin
error[E0040]: explicit use of destructor method
  --> p1_fail_method.rs:13:7
   |
13 |     g.drop();
   |       ^^^^ explicit destructor calls not allowed
   |
help: consider using `drop` function
   |
13 -     g.drop();
13 +     drop(g);
   |

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0040`.

The caret sits on drop, not on the receiver: the value, the borrow, and the Drop impl are all fine - the call itself is the error. Note that the receiver had to be mut for the call to get this far in intent, since Drop::drop takes &mut self, but mutability changes nothing about the verdict. The compiler's own help already names the usual fix, the free function drop(g), covered as the second fix below.

The second failure: calling the trait function explicitly

Writing the call through the trait does not route around the ban. It is the same method, named in full:

Rust
struct Guard {
    name: &'static str,
}

impl Drop for Guard {
    fn drop(&mut self) {
        println!("dropping {}", self.name);
    }
}

fn main() {
    let mut g = Guard { name: "file lock" };
    Drop::drop(&mut g);
    println!("still in main");
}
Terminal
$ rustc --edition 2021 p2_fail_trait.rs -o p2_fail_trait.bin
error[E0040]: explicit use of destructor method
  --> p2_fail_trait.rs:13:5
   |
13 |     Drop::drop(&mut g);
   |     ^^^^^^^^^^ explicit destructor calls not allowed
   |
help: consider using `drop` function
   |
13 -     Drop::drop(&mut g);
13 +     drop(g);
   |

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0040`.

The underline now spans the whole Drop::drop path, and the header, label, and help are identical. Fully qualifying a call, importing the trait, or going through a generic bound does not change which method is being called - and it is the method, not the spelling, that is reserved.

First fix: let the scope end

Most explicit destructor calls are written because the value should be finished early - a lock released, a file closed, a buffer flushed. The idiomatic way to get that timing without any call is to give the value a scope that ends at that point:

Rust
struct Guard {
    name: &'static str,
}

impl Drop for Guard {
    fn drop(&mut self) {
        println!("dropping {}", self.name);
    }
}

fn main() {
    {
        let _g = Guard { name: "file lock" };
        println!("inside the block");
    }
    println!("after the block");
}
Terminal
$ rustc --edition 2021 p3_scope.rs -o p3_scope.bin
$ ./p3_scope.bin
inside the block
dropping file lock
after the block

The destructor ran exactly where the block closed - between the two prints - with no call in the source. Moving the value into a function, returning early, or ending the block all work the same way: pick the scope, and the drop follows it.

Second fix: move the value into std::mem::drop

When the value has to die at one statement in the middle of a longer scope, use the free function the compiler's help suggests. std::mem::drop is documented as a plain function that takes its argument by value - defined, in the documentation's words, as an empty function whose argument is moved in and therefore dropped before the function returns. It is not the destructor method: it never calls drop(&mut self) on a value that stays alive; it ends the value's life by taking ownership of it.

Rust
struct Guard {
    name: &'static str,
}

impl Drop for Guard {
    fn drop(&mut self) {
        println!("dropping {}", self.name);
    }
}

fn main() {
    let g = Guard { name: "file lock" };
    println!("before drop");
    std::mem::drop(g);
    println!("after drop");
}
Terminal
$ rustc --edition 2021 p4_mem_drop.rs -o p4_mem_drop.bin
$ ./p4_mem_drop.bin
before drop
dropping file lock
after drop

The drop line lands between the two prints - the destructor ran at the std::mem::drop statement, not at the end of main. Because the value moved into the call, it is gone afterward; that consequence has its own error, covered below. In the prelude, the same function is reachable as plain drop(g), which is the form the E0040 help rewrites to.

Third fix: drop in place, for manually managed storage only

There is one place a destructor really is run by hand: code that owns storage the compiler does not manage - a custom smart pointer, an allocator, a container built on raw pointers. For that case, std::ptr::drop_in_place executes the destructor of the pointed-to value. It is unsafe, and its documented safety conditions include that the pointer is valid for reads and writes, properly aligned, and non-null, and that using the value after the call - including letting it be dropped again - is undefined behavior. The standard way to meet that last condition for a value on the stack is to hold it in ManuallyDrop, which suppresses the automatic drop, leaving the manual one as the only one:

Rust
use std::mem::ManuallyDrop;

struct Guard {
    name: &'static str,
}

impl Drop for Guard {
    fn drop(&mut self) {
        println!("dropping {}", self.name);
    }
}

fn main() {
    let mut g = ManuallyDrop::new(Guard { name: "file lock" });
    println!("before drop_in_place");
    unsafe {
        std::ptr::drop_in_place(&mut *g);
    }
    println!("after drop_in_place");
}
Terminal
$ rustc --edition 2021 p5_drop_in_place.rs -o p5_drop_in_place.bin
$ ./p5_drop_in_place.bin
before drop_in_place
dropping file lock
after drop_in_place

Exactly one drop line prints: ManuallyDrop held the automatic drop back, and drop_in_place ran the destructor once, at the call. This is a tool for unsafe infrastructure code, not a replacement spelling for g.drop() in ordinary code - if the value is a normal owned variable, the first two fixes are the right ones, and reaching for a raw pointer adds safety obligations without adding capability.

The opposite request: preventing a drop is std::mem::forget

Sometimes the code that reaches for a destructor call actually wants the reverse - to keep the destructor from running, for example when ownership of the underlying resource has been handed to foreign code. That operation is legal and safe: std::mem::forget takes ownership of a value and forgets it without running its destructor, leaving any resources it manages to linger. It is the counterpart to keep separate from E0040: calling the destructor is banned, skipping it is allowed - because a leaked resource is a waste, while a double-dropped one is unsound.

Rust
struct Guard {
    name: &'static str,
}

impl Drop for Guard {
    fn drop(&mut self) {
        println!("dropping {}", self.name);
    }
}

fn main() {
    let g = Guard { name: "file lock" };
    std::mem::forget(g);
    println!("forgotten - no drop line follows");
}
Terminal
$ rustc --edition 2021 p6_forget.rs -o p6_forget.bin
$ ./p6_forget.bin
forgotten - no drop line follows

No drop line ever prints, and the program exits cleanly. forget is not a fix for E0040 - it produces the opposite outcome from dropping - but it is the answer when the real intent behind the manual call was suppressing the destructor, not running it.

Neighboring error: using the value after std::mem::drop is E0382

Because std::mem::drop takes its argument by value, the original binding is moved and cannot be used afterward. Trying gives a different error, not E0040:

Rust
struct Guard {
    name: &'static str,
}

impl Drop for Guard {
    fn drop(&mut self) {
        println!("dropping {}", self.name);
    }
}

fn main() {
    let g = Guard { name: "file lock" };
    std::mem::drop(g);
    println!("{}", g.name);
}
Terminal
$ rustc --edition 2021 p7_use_after_memdrop.rs -o p7_use_after_memdrop.bin
error[E0382]: borrow of moved value: `g`
  --> p7_use_after_memdrop.rs:14:20
   |
12 |     let g = Guard { name: "file lock" };
   |         - move occurs because `g` has type `Guard`, which does not implement the `Copy` trait
13 |     std::mem::drop(g);
   |                    - value moved here
14 |     println!("{}", g.name);
   |                    ^^^^^^ value borrowed here after move
   |
note: if `Guard` implemented `Clone`, you could clone the value
  --> p7_use_after_memdrop.rs:1:1
   |
 1 | struct Guard {
   | ^^^^^^^^^^^^ consider implementing `Clone` for this type
...
13 |     std::mem::drop(g);
   |                    - you could clone this value

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0382`.

If code after the drop still needs the value, the drop point is in the wrong place: move the std::mem::drop call later, or do the remaining work through a borrow taken before the call. A value cannot be both dropped and kept.

Decision summary

  • The header says "explicit use of destructor method" (E0040), with the caret on drop or on the whole Drop::drop path: a destructor was called by hand - remove the call and use scope end or std::mem::drop instead.
  • The intent was an early drop inside a longer function: std::mem::drop(value) at that statement, or a nested block whose end is the drop point.
  • The value lives in manually managed storage and its destructor must run in place: std::ptr::drop_in_place inside unsafe, with the value arranged - for example via ManuallyDrop - so it is not dropped a second time.
  • The intent was to stop the destructor from running at all: std::mem::forget(value) - legal, and the opposite of a drop.
  • The header instead says "borrow of moved value" (E0382) after a std::mem::drop call: the drop worked and the error is the later use - reorder the code, do not change the drop.

Checklist

  • No call to a drop method remains - neither value.drop() nor Drop::drop(&mut value), in any spelling.
  • The drop point chosen deliberately: scope end for the normal case, std::mem::drop for a named statement.
  • Nothing uses the value after std::mem::drop - the call moved it.
  • drop_in_place appears only in unsafe, manually managed storage, with a guarantee the value will not be dropped again.
  • forget used only where leaking the value's resources is the intended outcome.
  • The fixed program run, and the order of its output checked - the destructor's effect lands at the intended point, exactly once.

Sources

  • Rust, Error codes index: E0040 - the definition of the error: it is not allowed to manually call destructors in Rust; its example fails on x.drop() and its stated alternative is the std::mem::drop function, since drop is called automatically whenever a value goes out of scope.
  • The Rust Reference, Destructors - what a destructor is and when it runs: calling <T as Drop>::drop followed by recursively dropping fields, when a variable or temporary goes out of scope; it also names core::ptr::drop_in_place as the route when a destructor must be run manually, such as when implementing your own smart pointer.
  • Rust std, std::mem::drop - the free function's contract: it disposes of a value, is defined as an empty function taking the value by value, and the argument is therefore dropped before the function returns - the mechanism behind the second fix.
  • Rust std, std::ptr::drop_in_place - the unsafe in-place destructor: it executes the destructor of the pointed-to value, with safety conditions on validity, alignment, and non-nullness, and a warning that using the value after the call - including dropping it again - can cause undefined behavior.
  • Rust std, std::mem::forget - the suppression route: it takes ownership and forgets the value without running its destructor, and its documentation directs readers who want the destructor run to mem::drop instead.

Keep Exploring