3 ms·
> The solution was that I had to write a macro that extracted Y from S and call that macro every time I wanted to pass Y to a function. Why does that require a
by duped 5y ago
> The solution was that I had to write a macro that extracted Y from S and call that macro every time I wanted to pass Y to a function.
Why does that require a macro?
```
struct S {
x:X,
y:Y
}
fn do_with_y (y:&mut Y) {}
fn do_with_s (y:&mut S) {}
fn main_program (mut s: S) {
do_with_s(&mut s);
do_with_y(&s.y);
/* ... */
}
```
I prefer seeing code like this rather than wrapping it in abstractions and macros/helpers because it's not particularly verbose and very clear where the borrowing happens ; you'd see the same in C++ or C, to be honest.
- robalni 5y agoI made a macro because it was more complicated than just S.Y. It was more like `Something.get_stuff().unwrap().things`