Hi,
I'm using a BTreeMap to keep track of distances in a path finding algorithm and that is all working fine so far. As usual now I then cargo clippy
as I've learnt a lot from its suggestions however this one won't compile so wondering if there's a better solution.
warning: usage of `contains_key` followed by `insert` on a `BTreeMap``
Which is sensible, and then suggests an way to use an Entry but that then hits the Borrow Checker.
Rather than paste the suggestion here, I've made a small contrived example in Playground
And the compile error from Clippy's suggestion:
error[E0502]: cannot borrow `distance` as immutable because it is also borrowed as mutable
--> src/main.rs:31:22
|
29 | if let std::collections::btree_map::Entry::Vacant(e) = distance.entry(posfar) {
| ---------------------- mutable borrow occurs here
30 | if let Some(dist) = can_get_to(posnear, posfar) {
31 | e.insert(distance[&posnear] + dist);
| ------ ^^^^^^^^ immutable borrow occurs here
| |
| mutable borrow later used by call
Which I understand - but is there a way to satisfy both Clippy and the Borrow Checker?