nicoburns / blessed-rs

A community guide to the Rust ecosystem
https://blessed.rs
1.26k stars 72 forks source link

Add scopeguard #127

Open FeldrinH opened 2 months ago

FeldrinH commented 2 months ago

A utility/language extension that I quite like and which seems to be fairly commonly used (looking at the numbers on crates.io) is scopeguard, which adds a helper function and convenience macro for deferred cleanup actions (similar to the defer statement in Go).

djc commented 2 months ago

28M recent downloads, so it qualifies as popular at least...

I ran into this recently but I'm honestly not much of a fan, feels more idiomatic (and not much more verbose) to just define a guard type with a Drop impl.

FeldrinH commented 2 months ago

I guess it's a matter of taste. I personally like it because it allows me to have cleanup logic in the method body, close to the rest of the method code, rather than in a separate Drop impl. I find it more readable than the alternatives for cleanup logic that is specific to one method.