Function scopeguard::guard

source ·
pub fn guard<T, F>(v: T, dropfn: F) -> ScopeGuard<T, F, Always>where
    F: FnOnce(T),
Expand description

Create a new ScopeGuard owning v and with deferred closure dropfn.

Examples found in repository?
examples/readme.rs (lines 18-21)
16
17
18
19
20
21
22
23
24
fn g() {
    let f = File::create("newfile.txt").unwrap();
    let mut 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();
}