defr

Crates.iodefr
lib.rsdefr
version0.1.0
sourcesrc
created_at2022-11-20 00:06:19.398113
updated_at2022-11-20 00:06:19.398113
descriptionGolang `defer` statements but in Rust.
homepage
repository
max_upload_size
id718759
size7,075
xian (rueyxian)

documentation

README

defr

Golang defer statements but in Rust.

Overview

A simple library that provides a convenient macro for wrapping expressions with the drop method.

What is defer?

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.

Difference

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();
}

Notes

Definitely not a replacement for implementing the Drop trait. In fact, in most cases, the RAII pattern is the way.

Commit count: 0

cargo fmt