4 ms·
I don't write Rust, but I never understood why graphs meant you need circular references. Doesn't it just come down to the question of who owns the node? If i
by underdeserver 3y ago
I don't write Rust, but I never understood why graphs meant you need circular references.
Doesn't it just come down to the question of who owns the node?
If it's a tree, and parents are never removed before children, just make the child owned by the parent and keep a weak reference to the parent.
If it's a general graph, and vertices can exist or not exist regardless of edges, keep a list of them independent of the edges, and keep weak references in the edges.
If it's a graph where a one or a few roots exist, and nodes exist as long as there's a path from a root node to them, that sounds like a classic use case for Rc<>.
Is there a common use case I'm missing?
- lumost 3y agoThings get tricky when you have a valid triangular relationship amongst equal objects. This comes up far more often than you’d expect.
- underdeserver 3y agoCan you give an example?