Trait std::ops::Drop1.0.0 [] [src]

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'.

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;
}

Required Methods

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