A RAII scope guard that will run a given closure when it goes out of scope,
even if the code between panics (assuming unwinding panic).
Defines the macros `defer!`, `defer_on_unwind!`, `defer_on_success!` as
shorthands for guards with one of the implemented strategies.
#[macro_use(defer)]externcrate scopeguard;usescopeguard::guard;fnf(){defer!(println!("Called at return or panic"));panic!();}usestd::fs::File;usestd::io::Write;fng(){let f =File::create("newfile.txt").unwrap();letmut file =guard(f,|f|{// write file at return or panic
let_= f.sync_all();});// Access the file through the scope guard itself
file.write_all(b"test me\n").unwrap();}fnmain(){f();g();}