[−][src]Function dispose::defer
pub fn defer<F: FnOnce()>(f: F) -> Disposable<F>
Defer an action until the end of a lexical scope.
This function returns a value that calls the provided closure when dropped, resulting in
functionality akin to try...finally
blocks or Swift's defer
blocks and Go's defer func
.
Examples
use dispose::defer; { let _d = defer(|| println!("Hello from defer()!")); println!("Hello, world!"); } // This prints the following: // Hello, world! // Hello from defer()!
A more pertinent example would be the use of defer with the ?
operator:
use dispose::defer; fn tryme() -> Result<(), ()> { let _d = defer(|| println!("Cleaning up...")); println!("Hello!"); let uh_oh = Err(())?; // Pretend this was a function that failed println!("You can't see me: {:?}", uh_oh); Ok(()) } println!("hi"); tryme().map_err(|()| println!("ERROR: Something went wrong.")).unwrap_err() // This prints the following: // Hello! // Cleaning up... // ERROR: Something went wrong.