ownership

Rust Local variables: destruction order

Initialized locals in the same drop scope are normally destroyed in reverse declaration order. This is useful when an acquired resource depends on an earlier one. Struct fields follow a different rule. Expected output (not executed): End second first Why: The two locals have the same drop scope; the later declaration is destroyed first. Common mistake: Assuming all destruction in Rust is last-in, first-out, including struct fields.

Formal Syntax & Structure

rust syntaxrust
{ let first = value; let second = value; }

Working Example

Example Snippetrust
struct Log(&'static str);impl Drop for Log {    fn drop(&mut self) { println!("{}", self.0); }}fn main() {    let _first = Log("first");    let _second = Log("second");    println!("End");}
Rust Reference