struct D(i32);
impl Drop for D {
fn drop(&mut self) { println!("dropped {}", self.0); }
}
fn main() {
{
D(0);
let x = D(1);
let _ = D(2);
D(3);
let y = D(4);
let _ = D(5);
}
}
It's deterministic but complicated. Temporaries which are not bound are dropped at the end of their temporary scope, and variables are dropped at the end of the block in reverse order of declaration. Moreover, let _ = ... does not bind (but let _name = ... does). So D(2) and D(5) are still temporaries.
Thus, in your example, the temporaries drop in increasing order, and the named variables drop in decreasing order.
The drop order of non-temporaries is documented and thus supposedly guaranteed. The drop order of temporaries (in particular those subject to temporary lifetime extension) is somewhat more grey, as far as I understand. But I doubt the order of your explicit example can change.
The Rust community is awesome! I had missed the part that declared variables drop in reverse order but other stuff drops in declaration order and I got multiple people giving references to the actual definition within half an hour!