Crates.io | defr |
lib.rs | defr |
version | 0.1.0 |
source | src |
created_at | 2022-11-20 00:06:19.398113 |
updated_at | 2022-11-20 00:06:19.398113 |
description | Golang `defer` statements but in Rust. |
homepage | |
repository | |
max_upload_size | |
id | 718759 |
size | 7,075 |
Golang defer
statements but in Rust.
A simple library that provides a convenient macro for wrapping expressions with the drop
method.
The meaning might vary but in the Golang context, defer
is the finalizer. Unlike C++ and Rust, Golang does not have destructor equivalents. More on defer statements.
defer
calls in Golang are pushed onto a stack, then run in a last-in-first-out manner.
func main() {
defer run_4th()
defer run_3rd()
run_1st()
run_2nd()
}
The expressions inside defr!
block remain procedural.
fn main() {
defr! {
run_3rd();
run_4th();
}
run_1st();
run_2nd();
}
Definitely not a replacement for implementing the Drop
trait. In fact, in most cases, the RAII pattern is the way.