Mutably borrow one struct field while borrowing another in a closure

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));

Leave a Comment