[go: up one dir, main page]

guard

Function 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)
16fn g() {
17    let f = File::create("newfile.txt").unwrap();
18    let mut file = guard(f, |f| {
19        // write file at return or panic
20        let _ = f.sync_all();
21    });
22    // access the file through the scope guard itself
23    file.write_all(b"test me\n").unwrap();
24}