rust-unofficial / patterns

A catalogue of Rust design patterns, anti-patterns and idioms
https://rust-unofficial.github.io/patterns/
Mozilla Public License 2.0
7.98k stars 363 forks source link

idioms dtor-finally should use "defer" library instead #104

Open bigdogs opened 3 years ago

bigdogs commented 3 years ago

The sample code in idioms dtor-finally cannot capture outside variable, which means it's useless in most cases

MarcoIeni commented 3 years ago

Sorry, I don't understand, can you provide an example or something like that, please? :)

Kushagra-0801 commented 3 years ago

@bigdogs the struct in the sample code is a unit struct. You could have fields in there too. In the worst case, you can just store a Boxed closure and execute it in the drop impl.