This simple network model yields the following error. What is the canonical way to get around this? Could it be that rust is ill suited for networks and graphs?
struct Node {
value: f32,
}
struct Edge {
node1: usize, // index in network.nodes
node2: usize,
}
struct Network {
nodes: Vec<Node>,
edges: Vec<Edge>,
}
fn update(network: &mut Network) {
for edge in &mut network.edges {
let nodes = &mut network.nodes;
let node1 = &mut (nodes[edge.node1]);
let node2 = &mut (nodes[edge.node2]);
if node1.value > node2.value {
node1.value = ...
...
}
}
}
error[E0499]: cannot borrow `*nodes` as mutable more than once at a time
--> src/main.rs:67:24
|
66 | let node1 = &mut (nodes[edge.node1]);
| ------ first mutable borrow occurs here
67 | let node2 = &mut (nodes[edge.node2]);
| ^^^^^^ second mutable borrow occurs here
...
70 | if node1.value > node2.value {
| --------- first borrow later used here