4 ms·
I've been wrestling with this very concept. I often find myself writing custom variants of datastructures and relatively uncommon datastructures, and a language
by plinkplonk 10y ago
I've been wrestling with this very concept. I often find myself writing custom variants of datastructures and relatively uncommon datastructures, and a language that makes this difficult isn't very motivating to get into.
What is really needed is good documentation on this specific aspect.
If on the hand if the messaging is "if you find yourself reaching for unsafe, it is likely you need to rethink your code's design" and then also "common datastructures are hard to write without unsafe" there is some slight dissonance there (imho, ime).
I'm not looking for 'blessed libraries' of prebuilt datastructures. I want to code up datastructures (e.g quadtrees, graphs etc), and good guidance on when exactly unsafe is the only way to get this done.
I don't really want to pause in the middle of a project and have to spend serious amounts of time coding up a (custom/unusual) datastructure just because there is no way without using unsafe (etc).
Right now there doesn't seem to be good guidance on this aspect (writing datastructures in Rust) which is presented as an "advanced topic" and "the wrong way / the hard way" to learn Rust. (there is the linked list book, which serves as a starting point, and using Cell etc seems to work - I'm still struggling - which is fine, that is how people learn.)
PS: None of the above is really a criticism of Rust the language or the team. I think Rust is great, I'm just a bit frustrated with the 'build common datastructures being "advanced" ' aspect, but in the end it is probably just that I haven't been able to wrap my head around Rust yet
- kernelbandwidth 10y agoIn the same way that we're really only starting to learn patterns that make good use of affine typing, I think this will get easier as we discover better (for Rust) design patterns. Which is basically what you're asking for; I think we're still in a mixture of discovering and documenting said patterns because some standard approaches don't work the same. So far, it seems to me that a lot of 'textbook' data structures require unsafe, at least to translate in a mostly 1:1 manner. I think more novel patterns will emerge around non-1:1 translations in safe code, which may push at least some data structures out of the 'advanced Rust' territory. I recently ran into this myself while implementing an order statistic red-black tree in (safe) Rust, which shouldn't be particularly complicated, but has been a good bit more verbose than C++ or Java would have been, primarily because I did a lot of manual bookkeeping to avoid unsafe. Still, it was enlightening and I very much enjoy using Rust.
- kazagistar 10y agoOne of the better Rust tutorials shows how to handle a number of different strategies for data structures, including using unsafe: "Learning Rust With Entirely Too Many Linked Lists". http://cglab.ca/~abeinges/blah/too-many-lists/book/ http://cglab.ca/~abeinges/blah/too-many-lists/book/
- Manishearth 10y agoI think the messaging is like this because folks approach Rust and often try to write Rust code as if it were C. You can do this in C++; you can't do this in Rust. unsafe is highly discouraged, but designing the innards of abstractions like datastructures is one of the places where you basically need it. Be aware that good generic datastructures are just as hard in C++. See https://news.ycombinator.com/item?id=13580418 https://news.ycombinator.com/item?id=13580418 . Naive datastructure implementations often have strict aliasing bugs or mess up on the destruction behavior. Using unsafe to write datastructures in Rust isn't particularly hard; it's just as tricky as it is in C++. Having to use unsafe is annoying, but it's a minor annoyance. The nomicon helps teach how you're supposed to use unsafe.