Cannot borrow `doors` as mutable because it is also borrowed as immutable

As the title says I am having an error in my program and I would like to know how to fix it.

This is the program:

This is the error:

error[E0502]: cannot borrow `doors` as mutable because it is also borrowed as immutable
   --> src/main.rs:107:5
    |
106 |     let open_door = doors.choose(&mut rng).unwrap();
    |                     ---------------------- immutable borrow occurs here
107 |     doors.retain(|&x| x != *open_door);
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
...
111 |     doors_2.retain(|&x| x != sit.choice|open_door);
    |                                         --------- immutable borrow later captured here by closure

PS: I do know what is wrong I just don't know the solution

Just dereference when you create open-door:

let open_door = *doors.choose(&mut rng).unwrap();
//              ^
//              Dereference here
1 Like

Or, in the more general case, turn the borrowed value into an owned one, so that it doesn't borrow from the collection any more. (This is doable for Clone and ToOwned types, as well as e.g. Cow, and types that provide inherent to_string(), to_vec(), etc. methods.)

1 Like

Thank you, solved (I am a bit of a noob I don't know why I didn't see that)