Search code examples
rustpetgraph

How can I iterate a Petgraph node's edges with the nodes they connect?


The edges function of a Petgraph Graph returns an iterator of edges. Each iteration then returns an EdgeReference which handily stores both nodes and the edge weight, which you can see if you debug print one. But unfortunately the EdgeReference members are all private, so you can't access them in your code.

So how can I iterate both the edges and nodes connected to a node? This seems like something that should be simple, but I haven't been able to find any example code out there.


Solution

  • edges returns a iterator for EdgeReference which implements the EdgeRef trait which has a source and target that return NodeIndex which can be used to get the node from the graph.