struct S {
foo: usize,
bar: Vec<f64>,
}
I have a function update_foo()
that only mutates foo. If I want to call it from another function (like update()
), if the call to update_foo()
is done while self.bar
is already borrowed, the borrow checker complains that
error[E0502]: cannot borrow
*self
as mutable because it is also borrowed as immutable
Is there any better way than modifying the signature of update_foo()
to take a mutable reference to the type of foo
instead of taking a mutable reference to self? This works, but this feels very ugly.
Playground
Code is pasted bellow to save you a click
impl S {
pub fn update(&mut self) {
for bar in &self.bar { // borrow immutably self.bar
self.update_foo(*bar as usize); // should only borrow mutably self.foo
}
}
fn update_foo(&mut self, value: usize) {
self.foo = value; // only touch self.foo
}
}