I having issue on my rust codes

I have a struct A, a method B of this struct who received a mutable self, and inside this method I create a closure that needs also to receive a mutable reference to A. Smth like this:
struct A {}
impl A {
pub fn B(&mut self) {
self.something(|| {
self.other_method_required_mutable();
});
}
}

What's the best way to do this? I can use several Arcs, but then it could get messy
You already invited:

ravi Gupta

Upvotes from:

that looks like a weird design, could you describe why you need this? What are you trying to accomplish?

shangels

Upvotes from:

tip: write three backticks in a line before and after code, to format it properly

yakitoriPB

Upvotes from:

you'd need to split self into parts and borrow them mutably. Then one method would use one part, another method - another part. You can't possibly borrow self mutably more than once
The only other way I see is to provide &mut self back into closure as argument
struct A {}
impl A {
pub fn B(&mut self) {
self.something(|this| {
this.other_method_required_mutable();
});
}
}
This will only work if you don't borrow any parts of self inside something at the moment you call provided closure

If you wanna answer this question please Login or Register