Statement Temporaries Drop At The Semicolon
Temporaries passed as call arguments drop at the end of their statement, while let-bound values live until scope end; a Drop impl makes the timing visible.
Code
struct Lease(&'static str);
impl Drop for Lease {
fn drop(&mut self) {
println!("dropped {}", self.0);
}
}
fn accept(lease: &Lease) {
println!("borrowing {}", lease.0);
}
fn main() {
// temporaries: the statement ends, the value dies at the semicolon
accept(&Lease("call argument"));
println!("-- statement ended --");
// let-bound values live until the end of the scope (or drop())
let bound = Lease("let bound");
println!("-- binding alive --");
drop(bound);
// nested block: value drops when the block closes
{
let inner = Lease("inner block");
println!("-- inner alive --");
}
println!("-- main ending --");
}What the demo drops
Lease prints dropped with its name from the Drop impl, making lifetimes visible. accept(&Lease(“call argument”)) shows the temporary rule: the value dies at the semicolon, so dropped call argument prints before the statement-ended marker.
A let binding lives longer: bound survives until the explicit drop(bound), printing binding alive first. The inner block behaves like a scope; inner block drops when the closing brace passes, before the main-ending line.
Caveats and details
- Temporaries in call arguments end with their statement.
- drop() ends a binding early without waiting for scope exit.
Interactive Tools for This Topic
FAQ
- When exactly does a temporary drop?
- At the end of its statement: the demo prints dropped call argument before the statement-ended line, while the let-bound value waits for its explicit drop.
Report a problem with this exampleOpens a pre-filled issue on GitHub.