|
|
|
|
|
by noctune
2123 days ago
|
|
>And deeply nested object hierarchies are a no-go, too, because of the inability to do "partial borrows" of just a single field of a struct. I'm not totally sure if this is what you mean, but FYI you can borrow multiple fields mutably using destructuring: struct Foo(u8, u8);
fn main() {
let mut bar = Foo(1, 2);
let Foo(ref mut x, ref mut y) = &mut bar;
*x += 1;
*y -= 1;
println!("{} {}", bar.x, bar.y);
}
|
|
So it often comes up that you might call several methods in a given scope. If even one of those mutably borrows one field of one sub-object, then you can't have any other borrows of that object anywhere else in that scope.
Newbies from other languages trip on that often enough that I used to see questions about it in r/rust fairly frequently.