std::ops::Drop - Rust
Description: API documentation for the Rust `Drop` trait in crate `std`.
Keywords: rust, rustlang, rust-lang, Drop

# Trait std::ops::Drop 1.0.0 [−] [src]

    #[lang = "drop"]
    pub trait Drop {
        fn drop(&mut self);
    }

Used to run some code when a value goes out of scope. This is sometimes called a 'destructor'.

When a value goes out of scope, it will have its `drop` method called if its type implements `Drop`. Then, any fields the value contains will also be dropped recursively.

Because of this recursive dropping, you do not need to implement this trait unless your type needs its own destructor logic.

Refer to the chapter on `Drop` in The Rust Programming Language for some more elaboration.

# Examples

## Implementing `Drop`

The `drop` method is called when `_x` goes out of scope, and therefore `main` prints `Dropping!`.

    struct HasDrop;

    impl Drop for HasDrop {
        fn drop(&mut self) {
            println!("Dropping!");
        }
    }

    fn main() {
        let _x = HasDrop;
    }Run

## Dropping is done recursively

When `outer` goes out of scope, the `drop` method will be called first for `Outer`, then for `Inner`. Therefore, `main` prints `Dropping Outer!` and then `Dropping Inner!`.

    struct Inner;
    struct Outer(Inner);

    impl Drop for Inner {
        fn drop(&mut self) {
            println!("Dropping Inner!");
        }
    }

    impl Drop for Outer {
        fn drop(&mut self) {
            println!("Dropping Outer!");
        }
    }

    fn main() {
        let _x = Outer(Inner);
    }Run

## Variables are dropped in reverse order of declaration

`_first` is declared first and `_second` is declared second, so `main` will print `Declared second!` and then `Declared first!`.

    struct PrintOnDrop(&'static str);

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

    fn main() {
        let _first = PrintOnDrop("Declared first!");
        let _second = PrintOnDrop("Declared second!");
    }Run

## Required Methods

### `fn drop(&mut self)`

Executes the destructor for this type.

This method is called implilcitly when the value goes out of scope, and cannot be called explicitly (this is compiler error E0040). However, the `std::mem::drop` function in the prelude can be used to call the argument's `Drop` implementation.

When this method has been called, `self` has not yet been deallocated. That only happens after the method is over. If this wasn't the case, `self` would be a dangling reference.

# Panics

Given that a `panic!` will call `drop` as it unwinds, any `panic!` in a `drop` implementation will likely abort.

## Implementations on Foreign Types

### `impl<T, A> Drop for RawVec<T, A> where
A: Alloc,`
[src]

#### `fn drop(&mut self)`
[src]

Frees the memory owned by the RawVec without trying to Drop its contents.

## Implementors

- [src]
  `impl<'a, T, F> Drop for DrainFilter<'a, T, F> where
  F: FnMut(&mut T) -> bool,`
- [src]
  `impl<'a, I> Drop for Splice<'a, I> where
  I: Iterator,`
- [src]
  `impl<T> Drop for Box<T> where
  T: ?Sized,`
- [src]
  `impl<T> Drop for IntermediateBox<T> where
  T: ?Sized,`
- [src]
  `impl<K, V> Drop for BTreeMap<K, V>`
- [src]
  `impl<K, V> Drop for std::collections::btree_map::IntoIter<K, V>`
- [src]
  `impl<'a, T> Drop for std::vec::Drain<'a, T>`
- [src]
  `impl<T> Drop for std::sync::Weak<T> where
  T: ?Sized,`
- [src]
  `impl<T> Drop for Arc<T> where
  T: ?Sized,`
- [src]
  `impl<T> Drop for Vec<T>`
- [src]
  `impl<T> Drop for std::rc::Weak<T> where
  T: ?Sized,`
- [src]
  `impl<'a> Drop for std::string::Drain<'a>`
- [src]
  `impl<T> Drop for VecDeque<T>`
- [src]
  `impl<T> Drop for LinkedList<T>`
- [src]
  `impl<T> Drop for std::vec::IntoIter<T>`
- [src]
  `impl<T> Drop for Rc<T> where
  T: ?Sized,`
- [src]
  `impl<'a, T> Drop for std::collections::vec_deque::Drain<'a, T> where
  T: 'a,`
- [src]
  `impl<'a, T> Drop for PeekMut<'a, T> where
  T: Ord,`
- [src]
  `impl<'a, K, V> Drop for EntryPlace<'a, K, V>`
- [src]
  `impl Drop for CString`
- [src]
  `impl<W: Write> Drop for BufWriter<W>`
- [src]
  `impl Drop for Select`
- [src]
  `impl<'rx, T: Send> Drop for Handle<'rx, T>`
- [src]
  `impl<T> Drop for Sender<T>`
- [src]
  `impl<T> Drop for SyncSender<T>`
- [src]
  `impl<T> Drop for Receiver<T>`
- [src]
  `impl Drop for Condvar`
- [src]
  `impl<T: ?Sized> Drop for Mutex<T>`
- [src]
  `impl<'a, T: ?Sized> Drop for MutexGuard<'a, T>`
- [src]
  `impl<T: ?Sized> Drop for RwLock<T>`
- [src]
  `impl<'a, T: ?Sized> Drop for RwLockReadGuard<'a, T>`
- [src]
  `impl<'a, T: ?Sized> Drop for RwLockWriteGuard<'a, T>`

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result
+
  Collapse/expand all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)