8 ms·
Against massive initial skepticism, I’m becoming very impressed with Rust. I’m now going to be ‘that guy’ - apologies. After years of being annoyed by Rust peo
by DerDangDerDang 6y ago
Against massive initial skepticism, I’m becoming very impressed with Rust. I’m now going to be ‘that guy’ - apologies.
After years of being annoyed by Rust people claiming C++ is dying or too unsafe or whatever, I expected this to be another C# - a sort of C-with-training-wheels for people who care more about productivity than perf. Something I could safely ignore.
This ‘borrow checker’ stuff sounds great for kids, but for disciplined experienced C++ guys it’s at best a non feature and at worst would just get in the way, like GC, right?
In fact, I’m finding the reduced mental overhead (not just from borrow, but from the language as a whole being smaller and more consistent) is a huge win. Even learning the language and stdlib as I go.
I’ll still use C++ proudly where it’s the best choice, but I’d put some effort into trying Rust first. As a cpp-master-race guy, that surprised the crap out of me.
- amelius 6y agoHave you tried writing self-referential data structures, like doubly linked lists?
- nchi3 6y agoSelf-referential data structures are a code smell in non-GCd languages, and I think I have neither used or written a doubly linked list since university. You can do both in unsafe rust though. But you probably shouldn't no matter what high level non-GCd language you use. /Another ex-cpp-master-race guy who has been impressed with Rust
- cycloptic 6y agoI see doubly-linked lists and other self-referential structures all the time in low-level code. Boost even has a whole library dedicated to them: http://www.boost.org/libs/intrusive http://www.boost.org/libs/intrusive
- steveklabnik 6y agoFor a really interesting take on this area, see http://dtrace.org/blogs/bmc/2018/09/28/the-relative-performance-of-c-and-rust/ http://dtrace.org/blogs/bmc/2018/09/28/the-relative-performa... Specifically, the discussion about intrusive vs non-intrusive data structures and their frequency.
- cycloptic 6y agoIt would be interesting to see how that measures up in a different test where the accesses are all from a cold cache.
- nchi3 6y agoYeah, hence high level non-GCed language. Using heap allocated linked lists is rarely the way to go if you're not developing for embedded. However, if you're developing for embedded you'll probably want to be familiar with unsafe either way if you're going to use Rust. Regarding self-referencing: Rust encourages you to design differently, which I've found leads to nicer and more thought out designs most of the time. Not all algorithms are possible (or efficient) without it though, and for those you'll have to use unsafe if you _really_ have to implement them yourself.
- Silhouette 6y agoWhy would you write your own basic data structures when the standard library provides them for you? I haven't written an implementation of a doubly-linked list by hand in a very long time. If your point was about explicit ownership being a little more tricky with self-referential structures, that's true, though the existence of std::collections::LinkedList shows that it's entirely possible to manage these structures and at least you still get the benefits of explicit ownership for the structure and the data within it.
- amelius 6y ago> Why would you write your own basic data structures when the standard library provides them for you? I think the problem with this view is that Rust is not being used as a true programming language (like for implementing algorithms), but more as a "glue language". If all the interesting stuff has to be done in libraries in "unsafe" mode, and all the uninteresting stuff (gluing) is done with borrow checking, then Rust is more a glue language. Which is fine. But don't call it a general purpose programming language.
- Silhouette 6y agoI respectfully disagree. Using tools provided by the standard library is common practice in any general purpose programming language. It just means that the algorithms you're implementing can concentrate on the novel aspects that provide additional value and not get bogged down with reinventing wheels for the basic implementation details. Also, you seem to be suggesting that it's necessary to use unsafe code to implement these data structures in Rust. For something like the doubly-linked lists that were mentioned in an earlier comment, it usually suffices to use tools like Option and Rc for the links and to encapsulate the logic that manages those links inside functions like push_back and pop_back that you are surely defining to mutate your data structure anyway. Is there something specific you have in mind where this kind of strategy doesn't work and unsafe code is the only solution?
- steveklabnik 6y agoSomeone above suggests boot::intrusive. Do you also think that means C++ is a "glue language"?
- DerDangDerDang 6y agoI have not. Does the borrow checker make things difficult? Isn’t there a weak_ptr equivalent? Though honestly, in C++ it is very rare I use something other than vector (potentially wrapped for specific use case, like boost flat_map)
- steveklabnik 6y agoThere is a weak_ptr equivalent; the borrow checker makes it basically impossible to use references to implement it. You can still do it with raw pointers exactly like you would in C++ too. (And yes, it's a niche data structure, vecs are far more common, but it's people's go-to "the borrow checker restricts you too much" example anyway.)
- DerDangDerDang 6y agoThanks, that’s illuminating. Seems a counter productive go-to argument given the amount of effort we (c++ coders) generally put in to avoiding non-adjacent memory access :)
- steveklabnik 6y agoYou would think so, but it's sort of just become a standard thing on forums, someone claims that linked lists are impossible, someone else talks about how you can do them in various ways (possibly linking to the "Learning Rust with Too Many Linked Lists" book, which is awesome, by the way), and how you don't actually need them in real code, followed by someone saying that this is just Rust stockholm syndrome and they use linked lists all the time... just the fun of being on forums :)
- DerDangDerDang 6y agoHaha, forums gonna forum I guess. Reminds me of the “but how can I trust std containers to allocate for me? Anything could happen!” crowd in the 90s and early 2000s. FWIW I think the Rust community has done itself a disservice by focusing on the safety benefits. Const by default and move by default are much bigger deals in the day to day coding experience, but it’s hard to get across just how awesome they are. That book looks great, thanks - should take me through a few corners I’ve yet to meet while keeping me entertained.
- deleted 6y ago[deleted]