3 ms·
How is std::unique_ptr different from Rust's Box? Everything you said applies to Rust. Also, I definitely would not call std::unique_ptr (or Box) a GC.
by maleldil 4y ago
How is std::unique_ptr different from Rust's Box? Everything you said applies to Rust.
Also, I definitely would not call std::unique_ptr (or Box) a GC.
- steveklabnik 4y agoThey’re the same at a high level, but when you get into the details, there are different. A famous one is that uniq_ptr can’t be passed in registers (mentioned upthread) for ABI reasons, but Box doesn’t have that restriction. Another one is what happens when you move a value out, in C++ it’s in an “indeterminate state”, but in Rust you can’t access a value after move. Which means that you can get a null pointer exception in C++, but not in Rust, and means that destructors for uniq_ptr have to check for this case, whereas in Rust they don’t.
- maleldil 4y agoI understand that. I was talking about the high-level idea of using RAII to manage memory. Parent was talking as if that was unique to C++, while Rust also has it and does it better, for the reasons you stated. Rust defaulting to move-semantics and requiring explict copies is one of the best decisions in the language.
- gpderetta 4y agoNote that the ABI issue with trivial types is not a language issue but a gcc/clang backward compatibility issues with their own ABI. A bit of forward thinking could have prevented the issue (like annotating the type when it was first introduced).