How to get the source and target nodes of an petgraph::graph::EdgeReference?

How do I access the source and destination nodes from a petgraph::graph::EdgeReference? I want to iterate over all the edges, so I used graph.edge_references() method and then in each iteration I want to get the source and destination nodes of the edge. A minimal working example is -

use petgraph::Graph;
use petgraph::visit::IntoEdgeReferences;
use petgraph::visit::IntoNodeIdentifiers;
use petgraph::graph::IndexType;
use petgraph::graph::NodeIndex;

fn main() {
    let mut graph = Graph::<u32, u32>::new();
    graph.extend_with_edges(&[ (1, 2), (1, 3)]);
    for e in graph.edge_references() {
        println!("{:?}", e);
        // Do things with e.nodes

In the above example I want to access e.nodes but source and target are not implemented for petgraph::graph::EdgeReference and e.nodes is private.

Please let me know if there is a better way to iterate over edges or access the source and target nodes of an EdgeReference.

Sorry, I just realized I had to import petgraph::visit::EdgeRef trait. if I add use petgraph::visit::EdgeRef at the start it works.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.