1.0.0[][src]Trait un_algebra::tests::float::Drop

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

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

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!");
}

Required methods

fn drop(&mut self)

Executes the destructor for this type.

This method is called implicitly 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.

Loading content...

Implementations on Foreign Types

impl<T> Drop for SyncSender<T>[src]

impl<W> Drop for BufWriter<W> where
    W: Write
[src]

impl<'_, T> Drop for RwLockWriteGuard<'_, T> where
    T: ?Sized
[src]

impl<'_, T> Drop for RwLockReadGuard<'_, T> where
    T: ?Sized
[src]

impl<T> Drop for Receiver<T>[src]

impl<T> Drop for Sender<T>[src]

impl<T> Drop for Mutex<T> where
    T: ?Sized
[src]

impl<'_, T> Drop for MutexGuard<'_, T> where
    T: ?Sized
[src]

impl Drop for Condvar[src]

impl Drop for CString[src]

impl<T> Drop for RwLock<T> where
    T: ?Sized
[src]

impl<'f> Drop for VaListImpl<'f>[src]

impl Drop for Waker[src]

impl<const N: usize, T> Drop for IntoIter<T, N> where
    [T; N]: LengthAtMost32
[src]

impl<'_, T, F> Drop for DrainFilter<'_, T, F> where
    F: FnMut(&mut T) -> bool
[src]

impl<T> Drop for VecDeque<T>[src]

impl<T> Drop for Rc<T> where
    T: ?Sized
[src]

fn drop(&mut self)[src]

Drops the Rc.

This will decrement the strong reference count. If the strong reference count reaches zero then the only other references (if any) are Weak, so we drop the inner value.

Examples

use std::rc::Rc;

struct Foo;

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

let foo  = Rc::new(Foo);
let foo2 = Rc::clone(&foo);

drop(foo);    // Doesn't print anything
drop(foo2);   // Prints "dropped!"

impl<'_, T> Drop for PeekMut<'_, T> where
    T: Ord
[src]

impl<'_> Drop for Drain<'_>[src]

impl<T> Drop for Weak<T> where
    T: ?Sized
[src]

fn drop(&mut self)[src]

Drops the Weak pointer.

Examples

use std::rc::{Rc, Weak};

struct Foo;

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

let foo = Rc::new(Foo);
let weak_foo = Rc::downgrade(&foo);
let other_weak_foo = Weak::clone(&weak_foo);

drop(weak_foo);   // Doesn't print anything
drop(foo);        // Prints "dropped!"

assert!(other_weak_foo.upgrade().is_none());

impl<'_, T, F> Drop for DrainFilter<'_, T, F> where
    F: FnMut(&mut T) -> bool
[src]

impl<T> Drop for Arc<T> where
    T: ?Sized
[src]

fn drop(&mut self)[src]

Drops the Arc.

This will decrement the strong reference count. If the strong reference count reaches zero then the only other references (if any) are Weak, so we drop the inner value.

Examples

use std::sync::Arc;

struct Foo;

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

let foo  = Arc::new(Foo);
let foo2 = Arc::clone(&foo);

drop(foo);    // Doesn't print anything
drop(foo2);   // Prints "dropped!"

impl<'a, T> Drop for DrainSorted<'a, T> where
    T: Ord
[src]

fn drop(&mut self)[src]

Removes heap elements in heap order.

impl<K, V> Drop for BTreeMap<K, V>[src]

impl<'_, I> Drop for Splice<'_, I> where
    I: Iterator
[src]

impl<T> Drop for LinkedList<T>[src]

impl<T> Drop for IntoIter<T>[src]

impl<K, V> Drop for IntoIter<K, V>[src]

impl<T> Drop for Box<T> where
    T: ?Sized
[src]

impl<T> Drop for Weak<T> where
    T: ?Sized
[src]

fn drop(&mut self)[src]

Drops the Weak pointer.

Examples

use std::sync::{Arc, Weak};

struct Foo;

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

let foo = Arc::new(Foo);
let weak_foo = Arc::downgrade(&foo);
let other_weak_foo = Weak::clone(&weak_foo);

drop(weak_foo);   // Doesn't print anything
drop(foo);        // Prints "dropped!"

assert!(other_weak_foo.upgrade().is_none());

impl<T> Drop for Vec<T>[src]

impl<'_, T> Drop for Drain<'_, T>[src]

impl<'_, T> Drop for Drain<'_, T>[src]

impl<'_, '_, '_> Drop for BacktraceFrameFmt<'_, '_, '_>[src]

impl Drop for TempPath[src]

impl Drop for TempDir[src]

impl Drop for Ast

A custom Drop impl is used for Ast such that it uses constant stack space but heap space proportional to the depth of the Ast.

impl Drop for ClassSet

A custom Drop impl is used for ClassSet such that it uses constant stack space but heap space proportional to the depth of the ClassSet.

impl Drop for Hir

A custom Drop impl is used for HirKind such that it uses constant stack space but heap space proportional to the depth of the total Hir.

Loading content...

Implementors

Loading content...