2 comments

[ 3.2 ms ] story [ 17.9 ms ] thread
Key takeaways: Rust 2021's closure minimal capture breaks RAII patterns when only Copy-type fields are used in structs with Drop impl. Even with impl Drop, closures may capture Copy fields instead of the whole struct — a surprising edge case. Fix requires explicit ownership transfer via let stats = self.stats to override closure's partial capture.
I agree, closure capturing has been rather confusing.

Fun fact is, the behavior used to be what the author expected: |thing| thing.field would capture the entire thing, not just the field.

This also used to be really annoying, so they changed it.

The 2021 Rust edition introduced partial capturing [1]. This greatly increased ergonomics, but it seems it has a hidden cost too. Unfortunate.

[1] https://doc.rust-lang.org/edition-guide/rust-2021/disjoint-c...