#Compiler claims I'm borrowing a struct

6 messages · Page 1 of 1 (latest)

scarlet sun
#

Right here you borrow:

let current_node: &Vertex = &set.set[lowest_dist.1.0][lowest_dist.1.1];
```and here you mutate set
```rs
set.set[x][y].distance = current_node.distance;
molten zephyr
#

but arent &x and x different from one another? why would this pose an issue?

scarlet sun
#

If x == lowest_dist.1.0 and y == lowest_dist.1.1 then that Vertex will be mutated while also borrowed, which is not allowed.

#

For this I'd either copy Vertex from set so it's not borrowed, or if copying is expensive, use std::mem::take and then after the loop, use std::mem::replace to put it back. Assuming you never have the condition above, this should do the same thing but compile.

molten zephyr
scarlet sun
#

What about Clone?