3 ms·
The easiest way to build graphs in Rust is to store all your graph nodes in a Vec, and use indices instead of references to refer to other nodes. Also a cache l
by kam 9y ago
The easiest way to build graphs in Rust is to store all your graph nodes in a Vec, and use indices instead of references to refer to other nodes. Also a cache locality win.
- zyxzevn 9y agoThat is what I concluded too. And if your nodes can be of different types, you probably need to make them "enums" instead of "objects". So essentially you need these tricks to work with graphs, which means that your program design is really not as easy as with a garbage collected system, or manual memory management. I think that many new users have problems with complex graphs in Rust, especially cycled graphs.
- foldr 9y agoThis is really the only practical way to do it, yes. But it's gross and means that you have to write custom code to print the data structures. That might sound trivial, but the ability to autoderive pretty printers is super useful when debugging.
- steveklabnik 9y agoOr just use petgraph, which does this for you.
- foldr 9y agoThat's fine if you're actually running a bunch of graph algorithms, but it's not a good fit for, e.g., a parse tree where each node has a link to its parent as well as links to its children. At least, I for one don't want to have to pull in a relatively large library just to define such a simple data structure.