macro_rules! defer {
($($tokens:tt)*) => { ... };
}Expand description
Ensure the given closure is executed once the surrounding scope closes
- Triggered despite panics
- Inspired by Golang’s
defer, Java’s finally and Ruby’sensure
Examples
use rivia::prelude::*;
let vfs = Vfs::memfs();
let file = vfs.root().mash("file");
assert_vfs_mkfile!(vfs, &file);
// Create a scope that will trigger defer's destructor
{
defer!(vfs.remove(&file).unwrap());
}
assert_vfs_no_exists!(vfs, &file);