4 ms·
Have you tried writing a doubly linked list? https://rcoh.me/posts/rust-linked-list-basically-impossible/ https://rcoh.me/posts/rust-linked-list-basically-impo
by amelius 8y ago
Have you tried writing a doubly linked list?
https://rcoh.me/posts/rust-linked-list-basically-impossible/ https://rcoh.me/posts/rust-linked-list-basically-impossible/
- deleted 8y ago[deleted]
- teacpde 8y agoI was doing adventofcode this year using (and learning) Rust and had to write a doubly linked list. Impossible is obviously an exaggeration but I admit it is painful to wrap my head around it at the beginning. I had to google a reference implementation and understanding why it needs to be done that way is also not straightforward. It is definitely not the best thing to try out Rust with, but to me it is the best to force myself to really think hard and grasp the idea of ownership. Once I figured it out the complex becomes reasonable and I started to appreciate the safety aspect of Rust. I also did the same puzzle with Go, it was such a breeze, it is as easy as to implement a doubly linked list in python/ruby/$yourfavoritescriptlanguage. But I start to dig Rust as it makes me think more and it feels like an eye opening experience.
- pzixel 8y ago1. Do you often try to implement data structures like this? In my practice, arrays are almost always better. 2. Beside that, you may argue that I use "Don't have therefore is not required" argument. But it contained partial truth: if you fight Rust program model then you have to do some extra steps to achieve it. Rust borrowing model considers one owner who can share its data among consumers. And this is damn common situation. I written a whole telegram bot with futures, using Arc container only once, to share my http client across the threads. If you fight GC in Java then you have same troubles: all this stuff like weak pointers, byte array storages, reflection, etc, etc... If you are trying to write imperatively in haskell... And so on. If you fight language ideas, then your code becomes more complicated. The answer here is that you shouldn't have to do it often.
- amelius 8y agoTo answer your first question: > 1. Do you often try to implement data structures like this? Any self-referential data-structure has this problem. So I guess my answer would be "yes".
- marcosdumay 8y agoIn my experience, there are plenty of things that are much harder in Rust than other languages. Those tend to fall on the "how you do things", instead of the "what you want to achieve" side of work, so often it's just a matter of stopping, asking yourself "can't the language help me somehow instead of making my life harder?", and adapting your ways. A few times you are not so lucky and have an actual need that Rust makes harder. Those exist, like in any other language, and I'd be happy if I knew how to avoid them.