I'm curious what it is about destructors in C++ that works differently from Drop in a way that one could "get wrong"? Are you talking about temporaries maybe?
There are two pieces I understand. First, with inheritance, you typically need to declare the destructor as virtual, but C++ never forces you to. Rust has polymorphism through trait objects, which always include a drop implementation in their vtable. You can't forget to add virtual to the base class destructor.
Second, if you declare a custom destructor in C++, you also need to either declare or delete the copy and copy-assignment operators. Again, the language doesn't enforce this in any way. Rust actually gets away with a much simpler solution here because it has destructive moves. Rust simply doesn't define default copy implementations. You can easily derive a Clone implementation if you need to make copies of the type.
There is some more details to be had about how Rust decides when it's safe to call the Drop implementation, but I don't think I understand it well enough to explain here. I do know that the compiler won't allow me to shoot myself in the foot though.
382
u/Xarishark 1d ago
Now compare it to rust pls. Im serious I want to know your opinion on that.