3 ms·
Rust doesn't currently support splitting borrows in closures, to my recent frustration.
by DC-3 6y ago
Rust doesn't currently support splitting borrows in closures, to my recent frustration.
- dathinab 6y agoIt does, just not the way you needed it. ;) Basically you can always go from `&'a mut A` to `(&'a mut Field1, &'a mut Field2,...)`. What doesn't work is if you have a method like `fn field1(&mut self) -> &mut Filed1` (and same for 2) and you want to do something like `let f1 = self.field1(); let f2 = self.field2()`. Because `field2` wants to borrow all of `self` instead of just `self.field1`. So what is currently missing are partial self borrows. This with another "special" case of `&mut` can lead to it not working in closures (the data a closure captures is like a implicit struct it carries along). The other special think I meant is that `&mut A` behaves in some cases "like" Copy without being copy. I.e. as long as the compiler can guarantee that the copy of (the pointer in) `&mut A` is consumed before the control flow returns it can copy `&mut A`. But this doesn't work with e.g. `struct Outer { f3: &mut A}`!!. This allows easier code but when combined with closures and similar can sometimes lead to problems where the "intuitive" code doesn't work (as your `&mut A` is now _in_ a closure which doesn't has that properties)!