I have following struct:
pub struct Foo {
bar: Bar, // bar is another struct defined by me
member: u8 // <-- this variable needs to be changed by `bar` struct
}
impl foo {
pub fn call_bar_function(&mut self) {
self.bar.bar_function(); // <-- I want to pass "foo" here as mutable
}
}
bar_function()
needs to change some fields of struct Foo
or call one of its functions. But to do that I need reference of Foo
in bar_function
function, but I cannot figure out how to accomplish that.
I have tried this but its giving me an error:
--> src\foo.rs:40:38
|
40 | self.bar.bar_function(&mut self);
| -------- ------------------- ^^^^^^^^^ second mutable borrow occurs here
| | |
| | first borrow later used by call
| first mutable borrow occurs here
Is there a nice "Rustean" way of doing what I am trying to accomplish?