defr 0.1.0

Golang `defer` statements but in Rust.
Documentation
  • Coverage
  • 0%
    0 out of 2 items documented0 out of 1 items with examples
  • Size
  • Source code size: 6.58 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 271.8 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 7s Average build duration of successful builds.
  • all releases: 7s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • rueyxian

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.