scopeguard

Rust crate for a convenient RAII scope guard that will run a given closure when it goes out of scope, even if the code between panics (assuming unwinding panic).

The defer! macro and guard are no_std compatible (require only core), but the on unwinding / not on unwinding strategies require linking to std. By default, the use_std crate feature is enabled. Disable the default features for no_std support.

Please read the API documentation here.

Minimum supported Rust version: 1.20

build_status crates

How to use

```rs

[macro_use(defer)]

extern crate scopeguard;

use scopeguard::guard;

fn f() { defer! { println!("Called at return or panic"); } panic!(); }

use std::fs::File; use std::io::Write;

fn g() { let f = File::create("newfile.txt").unwrap(); let mut file = guard(f, |f| { // write file at return or panic let _ = f.syncall(); }); // access the file through the scope guard itself file.writeall(b"test me\n").unwrap(); } ```

Recent Changes