golang-like defer provider
- add dependency into Cargo.toml
[dependencies]
defer = { git = "https://github.com/0x75960/defer-rs", branch = "master" }
- import and use in your code
macro(recommended)
#[macro_use(defer)]
extern crate defer;
use defer::Defer;
fn main() {
defer!({
println!("this will appear 4th!");
});
defer!({
println!("this will appear 2nd!");
println!("this will appear 3rd!");
});
println!("this will appear 1st!");
}
Basic
extern crate defer;
use defer::Defer;
fn main() {
let defer1 = Defer::register(|| -> () {
println!("this will appear 4th!");
});
let defer2 = Defer::register(|| -> () {
println!("this will appear 2nd!");
println!("this will appear 3rd!");
});
println!("this will appear 1st!");
}
- there are least one difference between go and this.
# | in this library | go(original) |
---|---|---|
1. executed at | leaving scope | leaving function |