The following code
struct DisjointUnionSetCost {
parent:Vec<usize>,
rank:Vec<usize>,
cost:Vec<usize>
}
impl DisjointUnionSetCost {
...
fn find_set(&mut self, v:usize) -> usize {
...
}
fn get_cost(&mut self, v:usize) -> usize {
self.cost[self.find_set(v)]
}
}
gives errors telling me that the immutable borrow of *self to access cost clashes with the mutable borrow of *self to call find_set(). Why? Should not the borrow checker understand that call to find_set(), and hence the mutable borrow of *self, completes before the array is accessed?
If I change to code of get_cost() to
fn get_cost(&mut self, v:usize) -> usize {
let set = self.find_set(v);
self.cost[set]
}
which I believe should be equivalent, then the borrow checker is happy