The code below defines a directed graph using petgraph
extern crate petgraph;
use petgraph::graph::DiGraph;
fn main() {
// Define the list of edges. Each edge is in the format (source, target, edge weight).
let edge_list = vec![(0, 1), (0, 2), (1, 3), (2, 3), (3, 4)];
// Declare the directed graph using the `from_edges` function.
let graph = DiGraph::<i32, i32>::from_edges(&edge_list);
// Display the edges and nodes of the graph.
for edge in graph.raw_edges() {
println!(
"Edge from {:?} to {:?} with weight {:?}",
edge.source(),
edge.target(),
edge.weight
);
}
}
The code above works correctly. However, when I add type annotation to the edge_list as shown in the code below, I get an error.
extern crate petgraph;
use petgraph::graph::DiGraph;
fn main() {
// Define the list of edges. Each edge is in the format (source, target, edge weight).
let edge_list: Vec<(i32, i32)> = vec![(0, 1), (0, 2), (1, 3), (2, 3), (3, 4)];
// Declare the directed graph using the `from_edges` function.
let graph = DiGraph::<i32, i32>::from_edges(&edge_list);
// Display the edges and nodes of the graph.
for edge in graph.raw_edges() {
println!(
"Edge from {:?} to {:?} with weight {:?}",
edge.source(),
edge.target(),
edge.weight
);
}
}
the trait bound `NodeIndex: From<i32>` is not satisfied
the trait `From<Ix>` is implemented for `NodeIndex<Ix>`
required because of the requirements on the impl of `Into<NodeIndex>` for `i32`
Please explain the cause of this error and provide any relevant documentation or resources.