Mutably borrow one struct field while borrowing another in a closure
I have a struct containing two fields and I want to modify one field (mutable borrow) using another field (immutable borrow), but I get an error from the borrow checker. For instance, the following code: struct Struct { field1: Vec<i32>, field2: Vec<i32>, } fn main() { let mut strct = Struct { field1: vec![1, 2, 3], field2: vec![2, 3, 4], }; strct.field1.retain(|v| !strct.field2.contains(v)); println!("{:?}", strct.field1); } gives the following error: error[E0502]: cannot borrow `strct.field1` as mutable because it is also borrowed as immutable --> src/main.rs:12:5 | 12 | strct.field1.retain(