---
source: https://iinventedlight.com/library/notes/rust-drop.md
title: "Run cleanup when a Rust value goes out of scope with Drop"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "MIT OR Apache-2.0 (The Rust Programming Language); MIT OR Apache-2.0 (Rust standard library); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: The Rust Programming Language, Running Code on Cleanup with the Drop Trait; Rust standard library, Trait Drop. MIT OR Apache-2.0; MIT OR Apache-2.0."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: Rust Drop trait and destructors"
tags: [rust, drop, destructor, smart-pointers]
client: Librarian
sources:
  - id: S1
    title: "Running Code on Cleanup with the Drop Trait"
    publisher: The Rust Programming Language
    url: https://doc.rust-lang.org/book/ch15-03-drop.html
    accessed: 2026-11-19
    license: "MIT OR Apache-2.0"
  - id: S2
    title: "Trait Drop"
    publisher: Rust standard library
    url: https://doc.rust-lang.org/std/ops/trait.Drop.html
    accessed: 2026-11-19
    license: "MIT OR Apache-2.0"
---
# Run cleanup when a Rust value goes out of scope with Drop
**Answer:** The Drop trait lets you customize cleanup when a value is about to go out of scope. You can implement Drop on any type, and use that code to release resources like files or network connections.

## Destructors

When a value is no longer needed, Rust will run a destructor on that value. That destructor calls Drop::drop if the type implements the trait, then drop glue recursively calls the destructors of all the fields of this value. Because field destructors run automatically, you don't have to implement Drop in most cases. Implement it for types which directly manage a resource.

## Early cleanup

Occasionally you might want to clean up a value early. Call the std::mem::drop function provided by the standard library to force a value to be dropped. That function is different from the drop method in the Drop trait.

| Drop | destructor |
|---|---|
| Drop trait | Customize what happens when a value goes out of scope |
| Drop::drop | Custom code within the destructor |
| drop glue | Recursively calls the destructors of all contained fields |
| std::mem::drop | Function that force-drops a value early |

## See

- [rust-box-and-deref](https://iinventedlight.com/library/notes/rust-box-and-deref/)
- [rust-rc-refcell](https://iinventedlight.com/library/notes/rust-rc-refcell/)
- [rust-ownership-borrowing](https://iinventedlight.com/library/notes/rust-ownership-borrowing/)

## Claims

- id: C1
  claim: The Drop trait customizes what happens when a value is about to go out of scope.
  source: S1
  evidence: "customize what happens when a value is about to go out of scope"

- id: C2
  claim: Call std::mem::drop to force a value to be dropped early.
  source: S1
  evidence: "you have to call the std::mem::drop function"

- id: C3
  claim: Most types do not need a Drop impl because field destructors run automatically.
  source: S2
  evidence: "you don't have to implement Drop in most cases"

## Sources

- [S1] Running Code on Cleanup with the Drop Trait. The Rust Programming Language. https://doc.rust-lang.org/book/ch15-03-drop.html . License: MIT OR Apache-2.0. Accessed 2026-11-19.

- [S2] Trait Drop. Rust standard library. https://doc.rust-lang.org/std/ops/trait.Drop.html . License: MIT OR Apache-2.0. Accessed 2026-11-19.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Rust Programming Language, Running Code on Cleanup with the Drop Trait; Rust standard library, Trait Drop. MIT OR Apache-2.0; MIT OR Apache-2.0. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
