Loustak
February 17, 2021, 10:27am
1
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.
Loustak
February 17, 2021, 10:35am
2
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
system
Closed
May 19, 2021, 12:15am
4
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.