Iterator over struct field and self mutable

I'm trying to use struct composition in Rust.

You can see what I'm trying to do in the following example: Rust Playground

I understand the error, I can't borrow self (with the self.items.get()) and then try to mutate it with the self.manager.do_things(). But I can't find a way to make it work.

I found the answer: rust - Mutably borrow one struct field while borrowing another in a closure - Stack Overflow

Usually the borrow checker can distinguish between the different fields of a structure, but this doesn't work within closures (lambdas).

The trick is then to borrow the field outside the closure. Fixed playground: Rust Playground

1 Like

You may also want to check out this article on interprocedural conflicts, which is a more general form of the problem you ran into.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.