Usually the borrow checker can distinguish between the different fields of a structure, but this doesn’t work within closures (lambdas).
Instead, borrow the second field outside the closure:
let field2 = &strct.field2;
strct.field1.retain(|v| !field2.contains(v));
More Related Contents:
- How to use struct self in member method closure
- How can I modify self in a closure called from a member function?
- Can’t borrow mutably within two different closures in the same scope
- Why can’t I store a value and a reference to that value in the same struct?
- Cannot move out of value which is behind a shared reference when unwrapping
- Why does refactoring by extracting a method trigger a borrow checker error?
- How do I return a reference to something inside a RefCell without breaking encapsulation?
- Temporarily move out of borrowed content
- Returning a reference from a HashMap or Vec causes a borrow to last beyond the scope it’s in?
- When does a closure implement Fn, FnMut and FnOnce?
- Passing mutable self reference to method of owned object
- Do mutable references have move semantics?
- Cannot borrow as immutable because it is also borrowed as mutable in function arguments
- Double mutable borrow error in a loop happens even with NLL on
- What are the options to end a mutable borrow in Rust?
- How to update-or-insert on a Vec?
- How do I convert a Rust closure to a C-style callback?
- “borrowed value does not live long enough” when using the builder pattern
- cannot borrow `self.x` as immutable because `*self` is also borrowed as mutable
- Temporary value is freed at the end of this statement [duplicate]
- How to use a struct’s member as its own key when inserting the struct into a map without duplicating it?
- How do I move out of a struct field that is an Option?
- Why does this mutable borrow live beyond its scope?
- Is there another option to share an Arc in multiple closures besides cloning it before each closure?
- Why can’t I reuse a &mut reference after passing it to a function that accepts a generic type?
- How do I pass a closure through raw pointers as an argument to a C function?
- Why does a mutable borrow of a closure through DerefMut not work?
- How does the Rust compiler know `Cell` has internal mutability?
- How does for syntax differ from a regular lifetime bound?
- Why does the Rust compiler request I constrain a generic type parameter’s lifetime (error E0309)?