I have a vector of elements which I traverse and check for some property (immutable). In some cases I would then perform a mutable operation on a single item in that vector followed by an update of the container. How would I do that?
struct A {
buffer: Vec<B>,
}
impl A {
fn update(&self) {}
}
struct B {}
impl B {
fn check(&self) -> bool { true }
fn do_stuff(&mut self) {}
}
fn main() {
let mut a = A { buffer: vec![B{}] };
for b in a.buffer.iter_mut() { // mutable borrow occurs here
if b.check() {
b.do_stuff();
a.update(); // imutable borrow occurs here
}
}
}
Alternatively, if I go for an immutable iter()
. Is there a possibility to turn it mutable for the scope of do_stuff
?