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

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

The `Drop` trait is 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, if it implements this trait, it will have its `drop` method called. Then any fields the value contains will also be dropped recursively.

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

# Examples

A trivial implementation of `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

Showing the recursive nature of `Drop`. 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

Because variables are dropped in the reverse order they are declared, `main` will print `Declared second!` and then `Declared first!`.

    struct PrintOnDrop(&'static str);

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

## Required Methods

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

A method called when the value goes out of scope.

When this method has been called, `self` has not yet been deallocated. If it were, `self` would be a dangling reference.

After this function is over, the memory of `self` will be deallocated.

This function 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.

# Panics

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

## Implementors

- `impl<K, V> Drop for std::collections::btree_map::IntoIter<K, V>`
- `impl<'a, 'b> Drop for std::string::Splice<'a, 'b>`
- `impl<'a, T> Drop for std::vec::Drain<'a, T>`
- `impl<'a, T> Drop for std::collections::vec_deque::Drain<'a, T> where
  T: 'a,`
- `impl<T> Drop for LinkedList<T>`
- `impl<T> Drop for VecDeque<T>`
- `impl<T> Drop for Vec<T>`
- `impl<'a, T> Drop for PeekMut<'a, T> where
  T: Ord,`
- `impl<'a, I> Drop for std::vec::Splice<'a, I> where
  I: Iterator,`
- `impl<K, V> Drop for BTreeMap<K, V>`
- `impl<T> Drop for std::vec::IntoIter<T>`
- `impl<'a> Drop for std::string::Drain<'a>`
- `impl<T> Drop for std::rc::Weak<T> where
  T: ?Sized,`
- `impl<T> Drop for Rc<T> where
  T: ?Sized,`
- `impl<T> Drop for Box<T> where
  T: ?Sized,`
- `impl<T> Drop for IntermediateBox<T> where
  T: ?Sized,`
- `impl<T> Drop for Arc<T> where
  T: ?Sized,`
- `impl<T> Drop for RawVec<T>`
- `impl<T> Drop for std::sync::Weak<T> where
  T: ?Sized,`
- `impl<'a, K, V> Drop for EntryPlace<'a, K, V>`
- `impl Drop for CString`
- `impl<W: Write> Drop for BufWriter<W>`
- `impl Drop for Select`
- `impl<'rx, T: Send> Drop for Handle<'rx, T>`
- `impl<T> Drop for Sender<T>`
- `impl<T> Drop for SyncSender<T>`
- `impl<T> Drop for Receiver<T>`
- `impl Drop for Condvar`
- `impl<T: ?Sized> Drop for Mutex<T>`
- `impl<'a, T: ?Sized> Drop for MutexGuard<'a, T>`
- `impl<T: ?Sized> Drop for RwLock<T>`
- `impl<'a, T: ?Sized> Drop for RwLockReadGuard<'a, T>`
- `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`)