pub struct OnDrop<F: FnOnce()> { /* private fields */ }
Expand description
A type which calls a function when dropped. This can be used to ensure that cleanup code is run even in case of a panic.
Note that this only works for panics that unwind
– any code within OnDrop
will be skipped if a panic does not unwind.
In most cases, this will just work.
§Examples
// This will print a message when the variable `_catch` gets dropped,
// even if a panic occurs before we reach the end of this scope.
// This is similar to a `try ... catch` block in languages such as C++.
let _catch = OnDrop::new(|| log("Oops, a panic occurred and this function didn't complete!"));
// Some code that may panic...
// ...
// Make sure the message only gets printed if a panic occurs.
// If we remove this line, then the message will be printed regardless of whether a panic occurs
// -- similar to a `try ... finally` block.
std::mem::forget(_catch);
Implementations§
Trait Implementations§
Auto Trait Implementations§
impl<F> Freeze for OnDrop<F>where
F: Freeze,
impl<F> RefUnwindSafe for OnDrop<F>where
F: RefUnwindSafe,
impl<F> Send for OnDrop<F>where
F: Send,
impl<F> Sync for OnDrop<F>where
F: Sync,
impl<F> Unpin for OnDrop<F>where
F: Unpin,
impl<F> UnwindSafe for OnDrop<F>where
F: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more