fn remove_selected(players: &mut Vec<Player>, ids: Vec<String>) {
let selected_players = select_players(players, ids); // takes a reference
let selected_players = selected_players
.into_iter()
.map(|p| p.name.as_str())
.enumerate()
.collect::<Vec<(usize, &str)>>();
// sort and reverse with pos as key
for (pos, _) in selected_players {
players.remove(pos);
}
}
error[E0502]: cannot borrow `*players` as mutable because it is also borrowed as immutable
--> src\main.rs:309:21
|
300 | let selected_players = select_players(players, ids)?;
| ------- immutable borrow occurs here
...
308 | for (pos, _) in selected_players {
| ------------------------ immutable borrow later used here
309 | players.remove(pos);
| ^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
I am trying to remove some indices from a mutable vector, however, I cannot find a way to work around the borrow checker. I really do not need the selected_players to be a view into players, so it can be some seperate, cloned values, but even if I try to clone selected_players or the string slices inside of it, it still doesn't let me remove those positions. I do need those string slices, because I want to print the names of the items that were removed in the terminal.