| Crates.io | defer-lite |
| lib.rs | defer-lite |
| version | 1.0.0 |
| created_at | 2021-08-10 02:00:39.236443+00 |
| updated_at | 2021-08-10 02:00:39.236443+00 |
| description | A lightweight high-performance implementation of Go's defer statement. |
| homepage | |
| repository | https://github.com/rodrigocfd/defer-lite |
| max_upload_size | |
| id | 434050 |
| size | 6,238 |
A Rust implementation of Go's defer statement as the defer! macro, which executes a block of code when the surrounding scope ends.
This crate focuses on providing a lightweight, high-performance, no_std implementation of the defer! macro.
Add the dependency in your Cargo.toml:
[dependencies]
defer-lite = "1.0.0"
Simplest example:
use defer_lite::defer; // import the defer! macro
fn main() {
defer! { println!("Second"); }
println!("First");
}
Multiple statements:
use defer_lite::defer;
fn main() {
defer! {
println!("Second");
println!("Third");
}
println!("First");
}
In Go, the defer code runs when the function exits. In this Rust implementation, the code runs when the surrounding scope ends – this makes it possible to use defer inside loops:
use defer_lite::defer;
fn main() {
defer! { println!("End"); }
println!("Before");
for i in 0..2 {
defer! { println!("Defer {}", i); }
println!("Loop {}", i);
}
println!("After");
}
Licensed under MIT license, see LICENSE.md for details.