std::ops::Drop - Rust (original) (raw)
Trait std::ops::Drop1.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 Languagefor some more elaboration.
The drop
method is called when _x
goes out of scope, and thereforemain
prints Dropping!
.
struct HasDrop;
impl Drop for HasDrop { fn drop(&mut self) { println!("Dropping!"); } }
fn main() { let _x = HasDrop; }Run
When outer
goes out of scope, the drop
method will be called first forOuter
, 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
_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
fn [drop](#tymethod.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.
Given that a panic! will call drop
as it unwinds, any panic!in a drop
implementation will likely abort.
`impl<T, A> Drop for RawVec<T, A> where
fn [drop](#method.drop)(&mut self)
[src]
Frees the memory owned by the RawVec without trying to Drop its contents.
impl<'a, T> Drop for std::vec::[Drain](../../std/vec/struct.Drain.html "struct std::vec::Drain")<'a, T>
impl<'a, T, F> Drop for std::vec::[DrainFilter](../../std/vec/struct.DrainFilter.html "struct std::vec::DrainFilter")<'a, T, F> where F: [FnMut](../../std/ops/trait.FnMut.html "trait std::ops::FnMut")([&mut ](../primitive.reference.html)T) -> [bool](../primitive.bool.html),
impl<T> Drop for [LinkedList](../../std/collections/linked%5Flist/struct.LinkedList.html "struct std::collections::linked_list::LinkedList")<T>
impl<K, V> Drop for std::collections::btree_map::[IntoIter](../../std/collections/btree%5Fmap/struct.IntoIter.html "struct std::collections::btree_map::IntoIter")<K, V>
impl<K, V> Drop for [BTreeMap](../../std/collections/btree%5Fmap/struct.BTreeMap.html "struct std::collections::btree_map::BTreeMap")<K, V>
impl<T> Drop for [Arc](../../std/sync/struct.Arc.html "struct std::sync::Arc")<T> where T: ?[Sized](../../std/marker/trait.Sized.html "trait std:๐:Sized"),
impl<'a, T> Drop for [PeekMut](../../std/collections/binary%5Fheap/struct.PeekMut.html "struct std::collections::binary_heap::PeekMut")<'a, T> where T: [Ord](../../std/cmp/trait.Ord.html "trait std::cmp::Ord"),
impl<'a, I> Drop for [Splice](../../std/vec/struct.Splice.html "struct std::vec::Splice")<'a, I> where I: [Iterator](../../std/iter/trait.Iterator.html "trait std::iter::Iterator"),
impl<T> Drop for [Vec](../../std/vec/struct.Vec.html "struct std::vec::Vec")<T>
impl<T> Drop for std::sync::[Weak](../../std/sync/struct.Weak.html "struct std::sync::Weak")<T> where T: ?[Sized](../../std/marker/trait.Sized.html "trait std:๐:Sized"),
impl<'a, T> Drop for std::collections::vec_deque::[Drain](../../std/collections/vec%5Fdeque/struct.Drain.html "struct std::collections::vec_deque::Drain")<'a, T> where T: 'a,
impl<T> Drop for [IntermediateBox](../../std/boxed/struct.IntermediateBox.html "struct std::boxed::IntermediateBox")<T> where T: ?[Sized](../../std/marker/trait.Sized.html "trait std:๐:Sized"),
impl<T> Drop for std::vec::[IntoIter](../../std/vec/struct.IntoIter.html "struct std::vec::IntoIter")<T>
impl<T> Drop for [Rc](../../std/rc/struct.Rc.html "struct std::rc::Rc")<T> where T: ?[Sized](../../std/marker/trait.Sized.html "trait std:๐:Sized"),
impl<T> Drop for std::rc::[Weak](../../std/rc/struct.Weak.html "struct std::rc::Weak")<T> where T: ?[Sized](../../std/marker/trait.Sized.html "trait std:๐:Sized"),
impl<T> Drop for [VecDeque](../../std/collections/vec%5Fdeque/struct.VecDeque.html "struct std::collections::vec_deque::VecDeque")<T>
impl<'a, T, F> Drop for std::collections::linked_list::[DrainFilter](../../std/collections/linked%5Flist/struct.DrainFilter.html "struct std::collections::linked_list::DrainFilter")<'a, T, F> where F: [FnMut](../../std/ops/trait.FnMut.html "trait std::ops::FnMut")([&mut ](../primitive.reference.html)T) -> [bool](../primitive.bool.html),
impl<'a> Drop for std:๐งต:[Drain](../../std/string/struct.Drain.html "struct std:๐งต:Drain")<'a>
impl<T> Drop for [Box](../../std/boxed/struct.Box.html "struct std::boxed::Box")<T> where T: ?[Sized](../../std/marker/trait.Sized.html "trait std:๐:Sized"),
impl<'a, K, V> Drop for [EntryPlace](../../std/collections/hash%5Fmap/struct.EntryPlace.html "struct std::collections::hash_map::EntryPlace")<'a, K, V>
impl Drop for [CString](../../std/ffi/struct.CString.html "struct std::ffi::CString")
impl<W: [Write](../../std/io/trait.Write.html "trait std::io::Write")> Drop for [BufWriter](../../std/io/struct.BufWriter.html "struct std::io::BufWriter")<W>
impl Drop for [Select](../../std/sync/mpsc/struct.Select.html "struct std::sync::mpsc::Select")
impl<'rx, T: [Send](../../std/marker/trait.Send.html "trait std:๐:Send")> Drop for [Handle](../../std/sync/mpsc/struct.Handle.html "struct std::sync::mpsc::Handle")<'rx, T>
impl<T> Drop for [Sender](../../std/sync/mpsc/struct.Sender.html "struct std::sync::mpsc::Sender")<T>
impl<T> Drop for [SyncSender](../../std/sync/mpsc/struct.SyncSender.html "struct std::sync::mpsc::SyncSender")<T>
impl<T> Drop for [Receiver](../../std/sync/mpsc/struct.Receiver.html "struct std::sync::mpsc::Receiver")<T>
impl Drop for [Condvar](../../std/sync/struct.Condvar.html "struct std::sync::Condvar")
impl<T: ?[Sized](../../std/marker/trait.Sized.html "trait std:๐:Sized")> Drop for [Mutex](../../std/sync/struct.Mutex.html "struct std::sync::Mutex")<T>
impl<'a, T: ?[Sized](../../std/marker/trait.Sized.html "trait std:๐:Sized")> Drop for [MutexGuard](../../std/sync/struct.MutexGuard.html "struct std::sync::MutexGuard")<'a, T>
impl<T: ?[Sized](../../std/marker/trait.Sized.html "trait std:๐:Sized")> Drop for [RwLock](../../std/sync/struct.RwLock.html "struct std::sync::RwLock")<T>
impl<'a, T: ?[Sized](../../std/marker/trait.Sized.html "trait std:๐:Sized")> Drop for [RwLockReadGuard](../../std/sync/struct.RwLockReadGuard.html "struct std::sync::RwLockReadGuard")<'a, T>
impl<'a, T: ?[Sized](../../std/marker/trait.Sized.html "trait std:๐:Sized")> Drop for [RwLockWriteGuard](../../std/sync/struct.RwLockWriteGuard.html "struct std::sync::RwLockWriteGuard")<'a, T>