---
title: "Understanding the Drop Trait in Rust"
description: "How the Drop trait works, when the destructor runs, and why implementing it by hand is rarer than it looks."
url: "https://mayorana.ch/en/blog/drop-trait-rust"
language: "en"
date: "2025-07-30"
author: "Mohamed Bennekrouf"
tags:
  - "rust"
  - "drop"
fr_version: "https://mayorana.ch/fr/blog/drop-trait-rust-fr.md"
---

# Understanding the Drop Trait in Rust

The `Drop` trait in Rust enables custom cleanup logic when a value goes out of scope, providing deterministic resource management similar to C++’s RAII (Resource Acquisition Is Initialization). It ensures memory safety and proper resource deallocation without a [garbage collector](https://mayorana.ch/en/blog/why-garbage-collector).

<div class="svg-container" style="margin:2rem 0;">
<svg class="mm11-fig" viewBox="0 0 800 240" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="Values are declared res1 then res2, and dropped in reverse LIFO order: res2 first, then res1, when the scope ends">
<style>
.mm11-fig{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .mm11-fig,[data-theme="dark"] .mm11-fig{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.mm11-fig .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.mm11-fig .acbox{fill:var(--box);stroke:var(--ac);stroke-width:2}
.mm11-fig .title{font:700 13px ui-sans-serif,system-ui,sans-serif;fill:var(--tx)}
.mm11-fig .body{font:600 12px ui-sans-serif,system-ui,sans-serif;fill:var(--tx)}
.mm11-fig .cap{font:11px ui-sans-serif,system-ui,sans-serif;fill:var(--mut)}
.mm11-fig .ac{fill:var(--ac)}
.mm11-fig path{stroke:var(--ln);stroke-width:1.5;fill:none}
</style>
<defs>
<marker id="mm11-arrow" markerWidth="8" markerHeight="8" refX="6" refY="4" orient="auto"><path d="M0,0 L8,4 L0,8 Z" style="fill:var(--ac)"/></marker>
</defs>
<text x="400" y="24" text-anchor="middle" class="title">Declaration Order</text>
<rect x="60" y="40" width="300" height="50" rx="8" class="box"/>
<text x="210" y="70" text-anchor="middle" class="body">let _res1 = Resource { id: 1 };</text>
<rect x="60" y="100" width="300" height="50" rx="8" class="box"/>
<text x="210" y="130" text-anchor="middle" class="body">let _res2 = Resource { id: 2 };</text>
<text x="400" y="160" text-anchor="middle" class="cap">scope ends → drop in reverse order</text>
<text x="620" y="24" text-anchor="middle" class="title">Drop Order</text>
<rect x="440" y="40" width="300" height="50" rx="8" class="acbox"/>
<text x="590" y="70" text-anchor="middle" class="body ac">1st: drop resource 2</text>
<rect x="440" y="100" width="300" height="50" rx="8" class="box"/>
<text x="590" y="130" text-anchor="middle" class="body">2nd: drop resource 1</text>
<path d="M360,65 L440,65" marker-end="url(#mm11-arrow)"/>
<path d="M360,125 L440,125" marker-end="url(#mm11-arrow)"/>
</svg>
</div>

## What is the Drop Trait?

The `Drop` trait defines a single method, `drop`, which is automatically called when a value is destroyed:

```rust
trait Drop {
    fn drop(&mut self);  // Called automatically when the value is destroyed
}
```

## How It Works

- **Automatic Invocation**: Rust calls `drop` when:
  - A variable goes out of scope.
  - Ownership is transferred (e.g., moved into a function).
  - Explicitly dropped via `std::mem::drop`.
- **LIFO Order**: Values are dropped in the reverse order of their declaration (stack-like behavior).

**Example: Basic Drop**:
```rust
struct Resource {
    id: u32,
}

impl Drop for Resource {
    fn drop(&mut self) {
        println!("Dropping resource {}", self.id);
    }
}

fn main() {
    let _res1 = Resource { id: 1 };  // Dropped second
    let _res2 = Resource { id: 2 };  // Dropped first
}
```

**Output**:
```
Dropping resource 2
Dropping resource 1
```

Three different events funnel into the same destructor call — and exactly one event bypasses it:

<div class="svg-container" style="margin:2rem 0;">
<svg class="mm11b-fig" viewBox="0 0 800 300" width="100%" style="height:auto;max-width:780px;display:block;margin:0 auto;" role="img" aria-label="Scope end, ownership move and mem drop all converge on the same destructor call, while mem forget consumes the value without ever running the destructor">
<style>
.mm11b-fig{--bg:#f8fafc;--box:#ffffff;--tx:#1e293b;--mut:#64748b;--ln:#cbd5e1;--ac:#FF6B00}
:root.dark .mm11b-fig,[data-theme="dark"] .mm11b-fig{--bg:#0f172a;--box:#1e293b;--tx:#f8fafc;--mut:#94a3b8;--ln:#475569}
.mm11b-fig .box{fill:var(--box);stroke:var(--ln);stroke-width:1.5}
.mm11b-fig .acbox{fill:var(--box);stroke:var(--ac);stroke-width:2}
.mm11b-fig .deadbox{fill:none;stroke:var(--mut);stroke-width:1.5;stroke-dasharray:4 3}
.mm11b-fig .title{font:700 13px ui-sans-serif,system-ui,sans-serif;fill:var(--tx)}
.mm11b-fig .body{font:600 12px ui-sans-serif,system-ui,sans-serif;fill:var(--tx)}
.mm11b-fig .cap{font:11px ui-sans-serif,system-ui,sans-serif;fill:var(--mut)}
.mm11b-fig .ac{fill:var(--ac)}
.mm11b-fig .mut{fill:var(--mut)}
.mm11b-fig path{stroke:var(--ln);stroke-width:1.5;fill:none}
</style>
<defs>
<marker id="mm11b-arrow" markerWidth="8" markerHeight="8" refX="6" refY="4" orient="auto"><path d="M0,0 L8,4 L0,8 Z" style="fill:var(--ln);stroke:none"/></marker>
<marker id="mm11b-arrowac" markerWidth="8" markerHeight="8" refX="6" refY="4" orient="auto"><path d="M0,0 L8,4 L0,8 Z" style="fill:var(--ac);stroke:none"/></marker>
</defs>
<!-- triggers -->
<rect x="20" y="40" width="175" height="62" rx="8" class="box"/>
<text x="107" y="66" text-anchor="middle" class="body">scope ends</text>
<text x="107" y="86" text-anchor="middle" class="cap">the closing brace</text>
<rect x="215" y="40" width="175" height="62" rx="8" class="box"/>
<text x="302" y="66" text-anchor="middle" class="body">ownership moved</text>
<text x="302" y="86" text-anchor="middle" class="cap">callee's scope ends instead</text>
<rect x="410" y="40" width="175" height="62" rx="8" class="box"/>
<text x="497" y="66" text-anchor="middle" class="body">std::mem::drop(v)</text>
<text x="497" y="86" text-anchor="middle" class="cap">end the scope early</text>
<rect x="605" y="40" width="175" height="62" rx="8" class="acbox"/>
<text x="692" y="66" text-anchor="middle" class="body ac">std::mem::forget(v)</text>
<text x="692" y="86" text-anchor="middle" class="cap">consumed, no cleanup</text>
<!-- Y-merge of the three normal triggers -->
<path d="M107,102 L107,140"/>
<path d="M302,102 L302,140"/>
<path d="M497,102 L497,140"/>
<path d="M107,140 L497,140"/>
<path d="M302,140 L302,180" marker-end="url(#mm11b-arrow)"/>
<!-- destructor runs -->
<rect x="180" y="180" width="245" height="62" rx="8" class="box"/>
<text x="302" y="206" text-anchor="middle" class="body">drop(&amp;mut self) runs</text>
<text x="302" y="226" text-anchor="middle" class="cap">then fields drop, memory freed</text>
<!-- forget path -->
<path d="M675,102 L675,180" style="stroke:var(--ac)" marker-end="url(#mm11b-arrowac)"/>
<rect x="560" y="180" width="230" height="62" rx="8" class="deadbox"/>
<text x="675" y="206" text-anchor="middle" class="body mut">destructor never runs</text>
<text x="675" y="226" text-anchor="middle" class="cap">file stays open, memory retained</text>
<!-- caption -->
<text x="400" y="272" text-anchor="middle" class="cap">A move does not skip cleanup: it relocates who is responsible for it.</text>
<text x="400" y="290" text-anchor="middle" class="cap">Only mem::forget removes the responsibility entirely, which is why it leaks.</text>
</svg>
</div>

## When to Implement Drop Manually

### 1. Resource Cleanup
For managing non-memory resources like files, sockets, or locks:

```rust
struct DatabaseConnection {
    // Connection details
}

impl Drop for DatabaseConnection {
    fn drop(&mut self) {
        self.close();  // Ensure connection is released
    }
}
```

### 2. Custom Memory Management
For integrating with FFI or unsafe code:

```rust
struct RawBuffer {
    ptr: *mut u8,
}

impl Drop for RawBuffer {
    fn drop(&mut self) {
        unsafe { libc::free(self.ptr as *mut _); }  // Manually free heap memory
    }
}
```

### 3. Logging/Telemetry
To track object lifecycle:

```rust
struct MetricsTracker {
    start: std::time::Instant,
}

impl Drop for MetricsTracker {
    fn drop(&mut self) {
        log::info!("Tracker dropped after {}ms", self.start.elapsed().as_millis());
    }
}
```

## Key Rules

- **No Explicit Calls**: Rarely call `drop` directly; use `std::mem::drop` to explicitly drop a value.
- **No Panics**: Avoid panicking in `drop`, as it can lead to double-drops or program aborts.
- **Auto Traits**: Types implementing `Drop` cannot be `Copy`.

## Drop vs. Copy/Clone

| **Trait** | **Purpose** | **Mutually Exclusive?** |
|-----------|-------------|-------------------------|
| `Drop`    | Cleanup logic | Yes (cannot be `Copy`) |
| `Copy`    | Bitwise copy | Yes |
| `Clone`   | Explicit deep copy | No |

## Advanced: #[may_dangle] (Nightly)
For generic types where `T` might not need dropping (unsafe):

```rust
unsafe impl<#[may_dangle] T> Drop for MyBox<T> {
    fn drop(&mut self) { /* ... */ }
}
```

## When Not to Use Drop

- **Simple Data**: No need for `Drop` if cleanup is handled by other types (e.g., `Box`, `Vec`).
- **Thread-Safety**: Use `Arc` + `Mutex` instead of manual locking in `drop`.

## `Drop`, summarised
**Use `Drop` for**:
- Resource cleanup (files, locks, memory).
- FFI/safety-critical guarantees.
- Debugging/profiling.

**Avoid**:
- Reimplementing logic provided by Rust (e.g., `Box`’s deallocation).
- Complex operations that could panic.

**Real-World Example**: The `MutexGuard` type uses `Drop` to release locks automatically:

```rust
{
    let guard = mutex.lock();  // Lock acquired
    // ...
}  // `guard` dropped here → lock released
```

`mem::forget` on a type with a `Drop` impl skips the destructor entirely. That is safe Rust —
leaking is not unsoundness — but it means an unclosed file or an unfreed buffer, so it's a
deliberate tool rather than an escape hatch.
