pub struct DropGuard<T, F>where
F: FnOnce(T),{ /* private fields */ }
drop_guard
)Expand description
Wrap a value and run a closure when dropped.
This is useful for quickly creating destructors inline.
§Examples
#![feature(drop_guard)]
use std::mem::DropGuard;
{
// Create a new guard around a string that will
// print its value when dropped.
let s = String::from("Chashu likes tuna");
let mut s = DropGuard::new(s, |s| println!("{s}"));
// Modify the string contained in the guard.
s.push_str("!!!");
// The guard will be dropped here, printing:
// "Chashu likes tuna!!!"
}
Implementations§
Source§impl<T, F> DropGuard<T, F>where
F: FnOnce(T),
impl<T, F> DropGuard<T, F>where
F: FnOnce(T),
Sourcepub const fn new(inner: T, f: F) -> DropGuard<T, F>
🔬This is a nightly-only experimental API. (drop_guard
)
pub const fn new(inner: T, f: F) -> DropGuard<T, F>
drop_guard
)Create a new instance of DropGuard
.
§Example
#![feature(drop_guard)]
use std::mem::DropGuard;
let value = String::from("Chashu likes tuna");
let guard = DropGuard::new(value, |s| println!("{s}"));
Sourcepub fn into_inner(guard: DropGuard<T, F>) -> T
🔬This is a nightly-only experimental API. (drop_guard
)
pub fn into_inner(guard: DropGuard<T, F>) -> T
drop_guard
)Consumes the DropGuard
, returning the wrapped value.
This will not execute the closure. This is implemented as an associated
function to prevent any potential conflicts with any other methods called
into_inner
from the Deref
and DerefMut
impls.
It is typically preferred to call this function instead of mem::forget
because it will return the stored value and drop variables captured
by the closure instead of leaking their owned resources.
§Example
#![feature(drop_guard)]
use std::mem::DropGuard;
let value = String::from("Nori likes chicken");
let guard = DropGuard::new(value, |s| println!("{s}"));
assert_eq!(DropGuard::into_inner(guard), "Nori likes chicken");
Trait Implementations§
Auto Trait Implementations§
impl<T, F> Freeze for DropGuard<T, F>
impl<T, F> RefUnwindSafe for DropGuard<T, F>where
T: RefUnwindSafe,
F: RefUnwindSafe,
impl<T, F> Send for DropGuard<T, F>
impl<T, F> Sync for DropGuard<T, F>
impl<T, F> Unpin for DropGuard<T, F>
impl<T, F> UnwindSafe for DropGuard<T, F>where
T: UnwindSafe,
F: UnwindSafe,
Blanket Implementations§
Source§impl<S> AssignWithType for S
impl<S> AssignWithType for S
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
Source§impl<C, E> EntryToVal<C> for Ewhere
C: Collection<Entry = E>,
impl<C, E> EntryToVal<C> for Ewhere
C: Collection<Entry = E>,
Source§type Val = <C as Collection>::Val
type Val = <C as Collection>::Val
Entry
in complex collections.
For example, in a HashMap
, while Entry
might be a ( key, value ) tuple, Val
might only be the value part.Source§fn entry_to_val(self) -> <E as EntryToVal<C>>::Val
fn entry_to_val(self) -> <E as EntryToVal<C>>::Val
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<'a, T> ToStringWith<WithDebug> for T
impl<'a, T> ToStringWith<WithDebug> for T
Source§fn to_string_with<'s>(&'s self) -> Cow<'s, str>
fn to_string_with<'s>(&'s self) -> Cow<'s, str>
Converts the type to a string using Debug formatting.
Source§impl<'a, T> ToStringWith<WithDebugMultiline> for T
impl<'a, T> ToStringWith<WithDebugMultiline> for T
Source§fn to_string_with<'s>(&'s self) -> Cow<'s, str>
fn to_string_with<'s>(&'s self) -> Cow<'s, str>
Converts the type to a string using Debug formatting.
Source§impl<Initial, Error, Final> TransitiveTryFrom<Error, Initial> for Final
impl<Initial, Error, Final> TransitiveTryFrom<Error, Initial> for Final
Source§impl<Error, Final, Initial> TransitiveTryInto<Error, Final> for Initial
impl<Error, Final, Initial> TransitiveTryInto<Error, Final> for Initial
Source§impl<C, Val> ValToEntry<C> for Valwhere
C: CollectionValToEntry<Val>,
impl<C, Val> ValToEntry<C> for Valwhere
C: CollectionValToEntry<Val>,
Source§fn val_to_entry(self) -> <C as CollectionValToEntry<Val>>::Entry
fn val_to_entry(self) -> <C as CollectionValToEntry<Val>>::Entry
Invokes the val_to_entry
function of the CollectionValToEntry
trait to convert the value to an entry.