3 ms·
`drop(&mut self)` means, strictly going by the types I could call drop twice! We want an `&own` that is like C++'s rvalue references (so we can still drop DSTs)
by Ericson2314 6y ago
`drop(&mut self)` means, strictly going by the types I could call drop twice! We want an `&own` that is like C++'s rvalue references (so we can still drop DSTs) so we can have `drop(&own self)`.
This correctly prevents one from calling Drop::drop twice without extra hacks, and demonstrates that the location is completely deinitialized and can be reinitialized to anything.
It is also dual to how, seemingly magically,
let x;
x = foo();
works without Rust thinking there is an old value of `x` to get rid of.
- saghm 6y ago> `drop(&mut self)` means, strictly going by the types I could call drop twice! Wouldn't calling a destructor on an object twice will also compile in C++? I don't really see this as an issue in either language; the solution is "don't ever call `drop`/destructors manually"
- steveklabnik 6y agoTo be clear, it won't compile in Rust either; you cannot invoke Drop::drop yourself: fn main() { let mut v = vec![1, 2, 3]; v.drop(); } building: Compiling playground v0.0.1 (/playground) error[E0040]: explicit use of destructor method --> src/main.rs:4:7 | 4 | v.drop(); | ^^^^ | | | explicit destructor calls not allowed | help: consider using `drop` function: `drop(v)` error: aborting due to previous error std::mem::drop, which it refers to, does take by owner. Your parent's point is that this is kind of a weird corner case that only applies to drop.
- saghm 6y agoOh, interesting! I had never tried to call it manually, so I didn't realize that it wouldn't let me.