5 ms·
The problem with Rust is that it is very hard to program interconnected graphs. Basic Rust does not allow cyclic references, which means that almost every grap
by zyxzevn 9y ago
The problem with Rust is that it is very hard to program interconnected graphs.
Basic Rust does not allow cyclic references, which means that almost every graph needs to use special tricks to make it work. These tricks are variants of reference counting. A trick that was copied directly from C++.
It feels like having to build a car with only a screwdriver and hammer. Each section of your graph needs to be managed separately. With non-safe rust or C++ you can put whole panels or sections on your car. And a garbage collector works like a robot: usually effortless.
- pornel 9y agoReference counting is not a trick in Rust. The standard Rc type is relatively easy to use and fast (since you can use a thread-unsafe variant safely). The tricky things are only if you want to have 2-way pointers without refcounting overhead.
- kam 9y agoThe 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.
- tdbgamer 9y agoTo be clear, cyclic references are just as much an issue in GCed languages. In Python I had to use weakref lib to ensure cyclically referenced objects are still GCed. Rust is the same in that aspect, you use the Rc builtin type and create a weakref for one of them.
- foldr 9y agoNo, GCs can handle cyclic references. You don't need weak references merely in order to ensure that a cyclic structure is eventually freed once it's become inaccessible. That's not to say that there might not sometimes be good reasons for using weak references in a GCed language, however.
- theseoafs 9y agoYou were being overpessimistic: Python's GC handles cyclic references perfectly well.