Trait nom::lib::std::ops::Drop 1.0.0[−][src]
#[lang = "drop"] pub trait Drop { pub fn drop(&mut self); }
Custom code within the destructor.
When a value is no longer needed, Rust will run a “destructor” on that value. The most common way that a value is no longer needed is when it goes out of scope. Destructors may still run in other circumstances, but we’re going to focus on scope for the examples here. To learn about some of those other cases, please see the reference section on destructors.
This destructor consists of two components:
- A call to
Drop::drop
for that value, if this specialDrop
trait is implemented for its type. - The automatically generated “drop glue” which recursively calls the destructors of the all fields of this value.
As Rust automatically calls the destructors of all contained fields,
you don’t have to implement Drop
in most cases. But there are some cases where
it is useful, for example for types which directly manage a resource.
That resource may be memory, it may be a file descriptor, it may be a network socket.
Once a value of that type is no longer going to be used, it should “clean up” its
resource by freeing the memory or closing the file or socket. This is
the job of a destructor, and therefore the job of Drop::drop
.
Examples
To see destructors in action, let’s take a look at the following program:
struct HasDrop; impl Drop for HasDrop { fn drop(&mut self) { println!("Dropping HasDrop!"); } } struct HasTwoDrops { one: HasDrop, two: HasDrop, } impl Drop for HasTwoDrops { fn drop(&mut self) { println!("Dropping HasTwoDrops!"); } } fn main() { let _x = HasTwoDrops { one: HasDrop, two: HasDrop }; println!("Running!"); }
Rust will first call Drop::drop
for _x
and then for both _x.one
and _x.two
,
meaning that running this will print
Running!
Dropping HasTwoDrops!
Dropping HasDrop!
Dropping HasDrop!
Even if we remove the implementation of Drop
for HasTwoDrop
, the destructors of its fields are still called.
This would result in
Running!
Dropping HasDrop!
Dropping HasDrop!
You cannot call Drop::drop
yourself
Because Drop::drop
is used to clean up a value, it may be dangerous to use this value after
the method has been called. As Drop::drop
does not take ownership of its input,
Rust prevents misuse by not allowing you to call Drop::drop
directly.
In other words, if you tried to explicitly call Drop::drop
in the above example, you’d get a compiler error.
If you’d like to explicitly call the destructor of a value, mem::drop
can be used instead.
Drop order
Which of our two HasDrop
drops first, though? For structs, it’s the same
order that they’re declared: first one
, then two
. If you’d like to try
this yourself, you can modify HasDrop
above to contain some data, like an
integer, and then use it in the println!
inside of Drop
. This behavior is
guaranteed by the language.
Unlike for structs, local variables are dropped in reverse order:
struct Foo; impl Drop for Foo { fn drop(&mut self) { println!("Dropping Foo!") } } struct Bar; impl Drop for Bar { fn drop(&mut self) { println!("Dropping Bar!") } } fn main() { let _foo = Foo; let _bar = Bar; }
This will print
Dropping Bar!
Dropping Foo!
Please see the reference for the full rules.
Copy
and Drop
are exclusive
You cannot implement both Copy
and Drop
on the same type. Types that
are Copy
get implicitly duplicated by the compiler, making it very
hard to predict when, and how often destructors will be executed. As such,
these types cannot have destructors.
Required methods
pub fn drop(&mut self)
[src][−]
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 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.
Note that even if this panics, the value is considered to be dropped;
you must not cause drop
to be called again. This is normally automatically
handled by the compiler, but when using unsafe code, can sometimes occur
unintentionally, particularly when using ptr::drop_in_place
.
Implementations on Foreign Types
impl Drop for Waker
[src][−]
impl<'f> Drop for VaListImpl<'f>
[src][−]
impl<T, const N: usize> Drop for IntoIter<T, N>
[src][−]
Implementors
impl<T> Drop for Sender<T>
impl<T> Drop for Sender<T>
impl Drop for PingSource
impl Drop for PingSource
impl Drop for Signals
impl Drop for Signals
impl<T> Drop for Sender<T>
impl<T> Drop for Sender<T>
impl<T> Drop for Receiver<T>
impl<T> Drop for Receiver<T>
impl Drop for SelectedOperation<'_>
impl Drop for SelectedOperation<'_>
impl<T> Drop for Injector<T>
impl<T> Drop for Injector<T>
impl<T: ?Sized + Pointable> Drop for Owned<T>
impl<T: ?Sized + Pointable> Drop for Owned<T>
impl Drop for LocalHandle
impl Drop for LocalHandle
impl Drop for Guard
impl Drop for Guard
impl<T: ?Sized> Drop for ShardedLockWriteGuard<'_, T>
impl<T: ?Sized> Drop for ShardedLockWriteGuard<'_, T>
impl Drop for WaitGroup
impl Drop for WaitGroup
impl<W: Write> Drop for DeflateEncoder<W>
impl<W: Write> Drop for DeflateEncoder<W>
impl<W: Write> Drop for ZlibEncoder<W>
impl<W: Write> Drop for ZlibEncoder<W>
impl<'a, R: Resources> Drop for AccessGuard<'a, R>
impl<'a, R: Resources> Drop for AccessGuard<'a, R>
impl<W: Write> Drop for Encoder<W>
impl<W: Write> Drop for Encoder<W>
impl Drop for Library
impl Drop for Library
impl<'a, R: RawMutex + 'a, T: ?Sized + 'a> Drop for MutexGuard<'a, R, T>
impl<'a, R: RawMutex + 'a, T: ?Sized + 'a> Drop for MutexGuard<'a, R, T>
impl<'a, R: RawMutex + 'a, T: ?Sized + 'a> Drop for MappedMutexGuard<'a, R, T>
impl<'a, R: RawMutex + 'a, T: ?Sized + 'a> Drop for MappedMutexGuard<'a, R, T>
impl<'a, R: RawMutex + 'a, G: GetThreadId + 'a, T: ?Sized + 'a> Drop for ReentrantMutexGuard<'a, R, G, T>
impl<'a, R: RawMutex + 'a, G: GetThreadId + 'a, T: ?Sized + 'a> Drop for ReentrantMutexGuard<'a, R, G, T>
impl<'a, R: RawMutex + 'a, G: GetThreadId + 'a, T: ?Sized + 'a> Drop for MappedReentrantMutexGuard<'a, R, G, T>
impl<'a, R: RawMutex + 'a, G: GetThreadId + 'a, T: ?Sized + 'a> Drop for MappedReentrantMutexGuard<'a, R, G, T>
impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> Drop for RwLockReadGuard<'a, R, T>
impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> Drop for RwLockReadGuard<'a, R, T>
impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> Drop for RwLockWriteGuard<'a, R, T>
impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> Drop for RwLockWriteGuard<'a, R, T>
impl<'a, R: RawRwLockUpgrade + 'a, T: ?Sized + 'a> Drop for RwLockUpgradableReadGuard<'a, R, T>
impl<'a, R: RawRwLockUpgrade + 'a, T: ?Sized + 'a> Drop for RwLockUpgradableReadGuard<'a, R, T>
impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> Drop for MappedRwLockReadGuard<'a, R, T>
impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> Drop for MappedRwLockReadGuard<'a, R, T>
impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> Drop for MappedRwLockWriteGuard<'a, R, T>
impl<'a, R: RawRwLock + 'a, T: ?Sized + 'a> Drop for MappedRwLockWriteGuard<'a, R, T>
impl Drop for Registration
impl Drop for Registration
impl Drop for Dir
impl Drop for Dir
impl<'d> Drop for Iter<'d>
impl<'d> Drop for Iter<'d>
impl Drop for InterfaceAddressIterator
impl Drop for InterfaceAddressIterator
impl Drop for PtyMaster
impl Drop for PtyMaster
impl<'a> Drop for AioCb<'a>
impl<'a> Drop for AioCb<'a>
impl Drop for SignalFd
impl Drop for SignalFd
impl Drop for TimerFd
impl Drop for TimerFd
impl<T> Drop for OnceBox<T>
impl<T> Drop for OnceBox<T>
impl<W: Write> Drop for Writer<W>
impl<W: Write> Drop for Writer<W>
impl<'a, W: Write> Drop for StreamWriter<'a, W>
impl<'a, W: Write> Drop for StreamWriter<'a, W>
impl<'a, T: Ord + Send> Drop for Drain<'a, T>
impl<'a, T: Ord + Send> Drop for Drain<'a, T>
impl<'a, T: Send> Drop for Drain<'a, T>
impl<'a, T: Send> Drop for Drain<'a, T>
impl<'a> Drop for Drain<'a>
impl<'a> Drop for Drain<'a>
impl<'data, T: Send> Drop for Drain<'data, T>
impl<'data, T: Send> Drop for Drain<'data, T>
impl Drop for ThreadPool
impl Drop for ThreadPool
impl Drop for Pool
impl Drop for Pool
impl<'pool, 'scope> Drop for Scope<'pool, 'scope>
impl<'pool, 'scope> Drop for Scope<'pool, 'scope>
impl<T, F, S> Drop for ScopeGuard<T, F, S> where
F: FnOnce(T),
S: Strategy,
impl<T, F, S> Drop for ScopeGuard<T, F, S> where
F: FnOnce(T),
S: Strategy,
impl Drop for DynamicLibrary
impl Drop for DynamicLibrary
impl<'a, T: 'a + Array> Drop for Drain<'a, T>
impl<'a, T: 'a + Array> Drop for Drain<'a, T>
impl<A: Array> Drop for SmallVec<A>
impl<A: Array> Drop for SmallVec<A>
impl<A: Array> Drop for IntoIter<A>
impl<A: Array> Drop for IntoIter<A>
impl Drop for DataDevice
impl Drop for DataDevice
impl Drop for DataOffer
impl Drop for DataOffer
impl Drop for PrimarySelectionDevice
impl Drop for PrimarySelectionDevice
impl Drop for PrimarySelectionOffer
impl Drop for PrimarySelectionOffer
impl Drop for MemPool
impl Drop for MemPool
impl Drop for ConceptFrame
impl Drop for ConceptFrame
impl<F: Frame> Drop for Window<F>
impl<F: Frame> Drop for Window<F>
impl<'a> Drop for ParseBuffer<'a>
impl<'a> Drop for ParseBuffer<'a>
impl<'a, W: Write + Seek> Drop for DirectoryEncoder<'a, W>
impl<'a, W: Write + Seek> Drop for DirectoryEncoder<'a, W>
impl<'a, W: Write + Seek, C: ColorType> Drop for ImageEncoder<'a, W, C>
impl<'a, W: Write + Seek, C: ColorType> Drop for ImageEncoder<'a, W, C>
impl Drop for ReadEventsGuard
impl Drop for ReadEventsGuard
impl Drop for Socket
impl Drop for Socket
impl Drop for UserData
impl Drop for UserData
impl<T: ?Sized> Drop for ThreadGuard<T>
impl<T: ?Sized> Drop for ThreadGuard<T>
impl Drop for WlEglSurface
impl Drop for WlEglSurface
impl Drop for Window
impl Drop for Window