
Understand it
When a value goes out of scope its Drop implementation runs, and remaining local bindings 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. At the outer closing brace, the remaining locals drop from last declared to first declared. The printed order is evidence of the ownership timeline. Binding a value with a leading underscore still owns it; the underscore only suppresses an unused-variable warning. If you need to release a value earlier, std::mem::drop(value) consumes it explicitly.
A destructor should not be where essential business logic hides, because callers cannot see that work from the return type. It should reliably release or finalize a resource. Also remember that moving a value transfers the eventual drop to its new owner; the old binding does not run a second destructor. Try moving one Noisy value into a helper function and record where its message appears. The exercise connects scope, movement, and cleanup without relying on a garbage collector or manual free call.
When several fields belong to one struct, their cleanup order follows the struct’s field declaration order after the struct’s own Drop method runs. Keep resource dependencies in mind if one field’s destructor relies on another resource.
Read the code
struct Noisy(&'static str);
impl Drop for Noisy {
fn drop(&mut self) {
println!("dropping {}", self.0);
}
}
fn main() {
let _first = Noisy("first");
let _second = Noisy("second");
{
let _inner = Noisy("inner");
println!("end of inner scope");
}
println!("end of outer scope");
}At each scope end, the last declared remaining local drops first.
Practice starter
struct Noisy(&'static str);
impl Drop for Noisy {
fn drop(&mut self) {
println!("dropping {}", self.0);
}
}
let a = Noisy("a");
let b = Noisy("b");
drop(a);
println!("after explicit drop");Try it
Predict the printed order before running it.
What success looks like: You can explain why dropping a prints before after explicit drop, and dropping b after it.
Check your understanding
Which of those messages prints last?
Reveal the answer
- dropping a
- after explicit drop
- dropping b
a is dropped early by hand; b still drops at the end of the scope.
Go deeper
Common misconception: “Locals drop in declaration order, so _first goes first.”
Remaining locals drop in reverse declaration order at scope end: _inner at the inner closing brace, then _second, then _first. An explicit drop(a) releases a value sooner; moving it to another owner or overwriting it also changes where cleanup occurs. Struct fields instead drop in field declaration order after their struct's own destructor.
- Mark each scope:
_firstand_secondlive until the closing brace ofmain._innerlives only inside the nested block. - Predict before checking: Write down the five lines the example prints, including where
dropping innerfalls relative toend of outer scope. - Compare with evidence: Run it:
end of inner scope,dropping inner,end of outer scope,dropping second,dropping first.
Transfer challenge: Remove drop(a); from the practice code and predict the new order. Then pass b to a helper function that takes Noisy by value and see where dropping b prints now.
Connections
- Copy & Clone: When a value duplicates instead of moving
- Lifetime Intuition: Why references stay valid
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Drop & Scope (courseId "ownership", lessonId "drop-order") Link: https://crabrix.com/learn/ownership/drop-order/ Goal: At scope end, remaining locals drop in reverse declaration order; struct fields drop in declaration order. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=ownership/drop-order) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.