4 ms·
You can also do something like: fn xy_mut(&mut self) -> (&mut f64, &mut f64) { let &mut Point { ref mut x, ref mut y } = self; (x,y) }
by Thiez 1y ago
You can also do something like:
fn xy_mut(&mut self) -> (&mut f64, &mut f64) {
let &mut Point { ref mut x, ref mut y } = self;
(x,y)
}
The fact of the matter is that doing mutable getters in Rust is a bit inconvenient at times. There are various workarounds.
- whytevuhuni 1y agoYou can do that with less syntax, this works just fine: fn xy_mut(&mut self) -> (&mut f64, &mut f64) { let Point { x, y } = self; (x, y) } Also, I assume this is just a super trivial example, because in this case I'd just make x and y public. After all, you're giving public access via these methods, and the caller can do anything it wants with them, so there's no real difference. And if the fields are public, it's much easier to take mutable access of multiple fields separately.