Mutable borrow in a loop
问题 I have the following code: struct Baz { x: usize, y: usize, } struct Bar { baz: Baz, } impl Bar { fn get_baz_mut(&mut self) -> &mut Baz { &mut self.baz } } struct Foo { bar: Bar, } impl Foo { fn foo(&mut self) -> Option<&mut Baz> { for i in 0..4 { let baz = self.bar.get_baz_mut(); if baz.x == 0 { return Some(baz); } } None } } Rust Playground It fails to compile with: error[E0499]: cannot borrow `self.bar` as mutable more than once at a time --> src/main.rs:23:23 | 23 | let baz = self.bar.get