Drop Order and Resource Acquisition Is Initialization
Values are cleaned up in the reverse of the order they were created, so the thing you set up last is torn down first. Tying a resource's life to a value's life — take it when you build the value, release it when the value dies — is a pattern old enough to have a clumsy name, but it is simply how you stop leaks. When you need cleanup in a particular order, you control it by controlling the order things were created.
Questions this Concept answers
- Why does reverse destruction order matter to a driver's teardown?
Drop and RAII as a feature
In Rust a value's cleanup code runs automatically when the value goes out of scope, and it runs on every way out of that scope: the closing brace, an early return, an error propagated with the questi…