News for hackers. Stuff that matters.
Re:Std::visit is everything wrong with modern C++ (2017) by DerDangDerDang on Saturday December 05, 2020 @10:30AM (#25315232)
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.

Re:Std::visit is everything wrong with modern C++ (2017) by amelius on Saturday December 05, 2020 @10:39AM (#25315303)
Have you tried writing self-referential data structures, like doubly linked lists?
Re:Std::visit is everything wrong with modern C++ (2017) by Silhouette on Saturday December 05, 2020 @11:02AM (#25315480)
Why 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.

Re:Std::visit is everything wrong with modern C++ (2017) by amelius on Saturday December 05, 2020 @11:20AM (#25315648)
> 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.

Re:Std::visit is everything wrong with modern C++ (2017) by Silhouette on Saturday December 05, 2020 @11:45AM (#25315870)
I 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?

Re:Std::visit is everything wrong with modern C++ (2017) by steveklabnik on Saturday December 05, 2020 @12:39PM (#25316442)
Someone above suggests boot::intrusive. Do you also think that means C++ is a "glue language"?
Re:Std::visit is everything wrong with modern C++ (2017) by xigoi on Saturday December 05, 2020 @12:45PM (#25316514)
So any language that doesn't force you to constantly reinvent the wheel is a “glue language”?
Re:Std::visit is everything wrong with modern C++ (2017) by j16sdiz on Saturday December 05, 2020 @11:21AM (#25315657)
I write my own basic structure when I need specific implementation for performance. Most of time its about memory layout or caching a specific internal pointer for some hot code.
Re:Std::visit is everything wrong with modern C++ (2017) by Silhouette on Saturday December 05, 2020 @11:39AM (#25315797)
Sure, but unless the high performance code you work with is very different in nature to the high performance code I've worked with, you probably have a small set of carefully implemented and probably very well optimised structures in your toolbox and write everything else using them. You do need to deal with the explicit ownership semantics in Rust when you're first implementing those tools, but that's a probably minor inconvenience up front and afterwards things mostly just work like the equivalent data structures in any other programming language, but with the added safety guarantees from the ownership system and borrow checker.
Re:Std::visit is everything wrong with modern C++ (2017) by bluecalm on Saturday December 05, 2020 @11:40AM (#25315818)
Double linked list is just an example. There are other both more useful and harder to generalize structures like various kinds of trees for example. Anyway, if your question is why implement your own data structures then it's very likely C/C++ isn't a proper tool for your job anyway.
Re:Std::visit is everything wrong with modern C++ (2017) by Silhouette on Saturday December 05, 2020 @11:54AM (#25315992)
There are other both more useful and harder to generalize structures like various kinds of trees for example.

Trees are usually easy because each node has a single parent all the way up to the root and ownership can follow the same hierarchy. More general graphs are a little more tricky, but the same techniques that you can use to implement a specific case like a DLL generalise. For example, in Rust, you can combine Option and Rc to handle, respectively, the intermediate states while the structure is being manipulated and the shared ownership semantics.

Re:Std::visit is everything wrong with modern C++ (2017) by nchi3 on Saturday December 05, 2020 @10:59AM (#25315456)
Self-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

Re:Std::visit is everything wrong with modern C++ (2017) by cycloptic on Saturday December 05, 2020 @11:34AM (#25315769)
I 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
Re:Std::visit is everything wrong with modern C++ (2017) by nchi3 on Saturday December 05, 2020 @01:15PM (#25316801)
Yeah, 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.

Re:Std::visit is everything wrong with modern C++ (2017) by steveklabnik on Saturday December 05, 2020 @12:38PM (#25316429)
For a really interesting take on this area, see http://dtrace.org/blogs/bmc/2018/09/28/the-relative-performa...

Specifically, the discussion about intrusive vs non-intrusive data structures and their frequency.

Re:Std::visit is everything wrong with modern C++ (2017) by cycloptic on Saturday December 05, 2020 @03:28PM (#25318002)
It would be interesting to see how that measures up in a different test where the accesses are all from a cold cache.
Re:Std::visit is everything wrong with modern C++ (2017) by DerDangDerDang on Saturday December 05, 2020 @12:26PM (#25316312)
I 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)

Re:Std::visit is everything wrong with modern C++ (2017) by steveklabnik on Saturday December 05, 2020 @12:37PM (#25316416)
There 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.)

Re:Std::visit is everything wrong with modern C++ (2017) by DerDangDerDang on Saturday December 05, 2020 @12:55PM (#25316615)
Thanks, 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 :)

Re:Std::visit is everything wrong with modern C++ (2017) by steveklabnik on Saturday December 05, 2020 @01:10PM (#25316765)
You 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 :)
Re:Std::visit is everything wrong with modern C++ (2017) by DerDangDerDang on Saturday December 05, 2020 @02:39PM (#25317607)
Haha, 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.

Re:Std::visit is everything wrong with modern C++ (2017) by steveklabnik on Saturday December 05, 2020 @03:21PM (#25317921)
You're welcome. I sorta kinda agree on the safety thing; I wrote a blog post that kind of kicked off a whole discussion about this a while back; https://brson.github.io/fireflowers/ is a really beautiful summary of all of that.
Re:Std::visit is everything wrong with modern C++ (2017) by DerDangDerDang on Saturday December 05, 2020 @05:07PM (#25318873)
That was a wonderful read.

After 20 years of C++ and about the same number of hours of Rust, Rust makes me feel like Fire Mario because when I come to do a strict correctness and memory efficiency pass after writing something - it’s already correct. I suspect this will have a compound interest-like effect, similar to good code coverage empowering change.