On this page
  1. Drop
  2. Availability
  3. Syntax & Overview
  4. Notes & Pitfalls
  5. Example

Drop

Prev: Clone | Up: builtins | Next: Iterator
Rel: builtins, Ownership, Borrowing & Lifetimes
Ref: Rust Docs — Drop
Tags: #public #prog #rust #builtins #tier/core


Availability

Built-in / Prelude (Prelude Trait (RAII)) — available with zero use imports.

Syntax & Overview

fn drop(&mut self)

Deterministic destructor automatically invoked when an owned value goes out of scope (in reverse declaration order).

Notes & Pitfalls

Example

struct Guard(&'static str);

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

fn main() {
    let g = Guard("lock");
    drop(g); // releases immediately before end of scope
}

Linked from

Related notes