Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It's really hard to determine what parts of comp sci are niche a-priori. Most of my current work involves recursive data structures (trees/graphs/linked lists).

I don't mind the callout that you need to use unsafe for recursive structures, but it would be useful to acknowledge that as part of the language design in the rust book. As a beginner you can easily pick up a simple LeeCode problem to learn the syntax and accidentally pick an impossible problem.



For the beginner there's "Learn Rust With Entirely Too Many Linked Lists"[0]. The main issue I have with them is they tend to optimize poorly on modern hardware even when they theoretically should be the right data structure to use (IMHO of course).

[0]: https://rust-unofficial.github.io/too-many-lists/


Yes, it is. It's one of the tough parts of the book, I cannot assume anything about the audience. That includes "they even know about recursive data structures," let alone "they want to write them."

In general, it's not possible to enumerate all negatives, but it is for all positives, so I try to say more about what is than what is not.

The issue is not graphs, trees, and lists. These are useful! It's implementing them the way that you do in CS 101 that is the issue.


First off, thank you for all of the work you've put into Rust! I think the challenge that you face is that if the problem is CS101 courses, then Rust will always be unfamiliar to the majority of practicing professionals.

Cyclic/recursive datastructures are common in web services, where one needs to model bidirectional relationships ( just think of friends on a social graph ). As well as several primitive datastructures such as the O(1) LRU cache.

Checking the Rust docs out, a dev who encounters this problem for the first time will eventually need

- "Reference Cycles Can Leak Memory" https://doc.rust-lang.org/book/ch15-06-reference-cycles.html -- caveat: what if ownership is truly bidirectional?

- "unsafe" https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html

- 5 different primitive lists which don't tell you how to make a friends graph: https://rust-unofficial.github.io/too-many-lists/infinity-do...

- Someone elses attempt: https://rcoh.me/posts/rust-linked-list-basically-impossible/

- Careful reading of the LinkedList implementation to see that unsafe is probably the best approach: https://doc.rust-lang.org/beta/src/alloc/collections/linked_...

I'm a little biased on this front, when I first picked up Rust I chose to tackle an O(1) LRU cache as a quick learning exercise. I ended up beating my head against a wall for 20 hours. However on my current project I'm analyzing the wikipedia dataset ( another bidirectional structure ).

It would be useful to have a canonical/standard way to address such programming tasks other than "don't" - particulary if the way to address them is substantially different from what someone would have learned in a different language.


You’re welcome!

Do you really think the majority of professionals have CS degrees? I’m not so sure. And beyond that, it would have to be “a majority with a degree” and “a majority that choose to implement data structures as an initial/early task.” I’m really not convinced the numbers actually play out that way.


Hmm playing off my own anecdote - I do not have a formal CS degree, but I ended up taking courses and reading a few of the typical books later on.

I think while my early experience biased me, the pain would have happened at one point or another eventually. I'd be surprised if the majority of web devs, and data science folks exploring rust wouldn't hit this problem at some point or another. As an example, one could easily run into this problem while writing an wiki,instagram, or twitter clone!


Sounds like we're opposing anecdotes; I do have a CS degree, but didn't try to start with data structures.

You may want to use something with those characteristics, sure, but that doesn't mean most users start by trying to implement, rather than use something existing.


Is there any work happening to make them easier to implement or is this just "how Rust is" and people have to live with it?


It’s not clear what would make it easier, and it’s also not something people are particularly crying out for. They’re not super common structures and as soon as someone makes a crate, you can use it and get on with life. And if you have to, you can just write what you would in C, so it’s not harder exactly, either.


I'd +1 a quick section in the book on when unsafe is the best choice using some referential datastructure ( along with potentially a few other examples where unsafe must be used )


In addition to the resources others pointed out, there is also the LinkedList collection in the std library: https://doc.rust-lang.org/std/collections/struct.LinkedList....

Although that may or may not be flexible enough for your needs


>I don't mind the callout that you need to use unsafe for recursive structures

You don't, really. Recursion is not the important part. The ownership model is hierarchical. If your structure is a directed tree with all links (pointers) flowing out from the root, it can easily be built. As soon have you have cycles or multiple parents, you'll need either unsafe or some ownership extension like shared ownership (Rc/Arc).

Singly linked list => Easy

Tree => Easy

Tree with backreferences => probably best to use unsafe

Graph => probably needs unsafe for best performance




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: