Start with the idea
When a value goes out of scope its Drop implementation runs, and values in a scope drop in reverse declaration order. This is deterministic — no collector decides later — which is what lets Rust manage files, locks, and sockets with the same mechanism it uses for memory.
Rust cleans up owned resources at predictable points. When a scope ends, its local values are dropped in reverse declaration order; nested scopes finish before their outer scope continues. Drop is a trait whose drop method runs during that cleanup. It can release a file, unlock a mutex guard, or close a connection. Memory is only one resource governed by the same rule.
Follow the Noisy example and predict the messages before running it. The inner value is created after the outer values and disappears at the inner closing brace.
Keep learning, for free
The rest of this lesson is waiting for you.
Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.
No payment details. Your account opens the complete Academy.