4 ms·
If you mean intrusive data structures, Rust just doesn't support them. Everyone still manages to write software in Rust just fine. (There's some early support f
by damnyou 7y ago
If you mean intrusive data structures, Rust just doesn't support them. Everyone still manages to write software in Rust just fine. (There's some early support for immovable data with `Pin`, but that's only exposed via async/await at the moment.)
Move constructors are one of the worst parts of C++ and being able to write movable, intrusive data structures is absolutely not worth the cost. If you do need one, Rust shows that it's better to sacrifice movability than introduce move constructors.
This is not uncommon -- there are some things you can do in C++ that you simply cannot do in Rust (either safely, or at all). Usually there's another, better way to achieve the same overall goals.
- asveikau 7y ago> Move constructors are one of the worst parts of C++ Move constructors fix a narrow problem, which is, when you have something like a vector append, how do you copy over all the previous elements as a "shallow" copy rather than a deep one? In C with realloc(), it's just assumed that memcpy works for that. With C++03 and earlier copying the elements could very well end up duplicating everything on the heap for no reason, then discarding the old copy. How does rust do this? What I am reading from googling is that every assignment is a move?
- damnyou 7y agoYes, every assignment of a non-Copy type is a move. (Copy types are basically primitive ones like ints, so not vectors etc.) The compiler prevents you from using the old variable at that point. Since Rust always enforces that a move is a memcpy, a vector reallocation is just a realloc() like in C.
- steveklabnik 7y agoSafe rust may not support them, but you can use unsafe to write them. There’s a bunch of implementations of intrusive collections on crates.io. Also, Pin is stable but async/await is not, so you’re backwards there :)