When I started with rust I had a lot of difficulties iterating over a vector that I had already borrowed mutably. I fixed it back then with swap_remove or index based looping and mutating the vector spot immediately. Which I find both to be hacky. However I created the following trait which I think is much more user friendly for especially new users.
pub type OtherIterMut<'a, T> = std::iter::Chain<IterMut<'a, T>, IterMut<'a, T>>;
pub trait Splitting<T: std::fmt::Debug> {
fn split_item_mut(&mut self, idx: usize) -> (&mut T, OtherIterMut<T>);
}
impl<T: std::fmt::Debug> Splitting<T> for Vec<T> {
fn split_item_mut(&mut self, idx: usize) -> (&mut T, OtherIterMut<T>) {
assert!(idx < self.len());
let (head, rest) = self.split_at_mut(idx);
let (item, tail) = rest.split_first_mut().unwrap();
let others = head.iter_mut().chain(tail.iter_mut());
(item, others)
}
}
In this way I can use:
let (item, others) = some_vector.split_item_mut(some_index);
for other in others {
other.some_field = item.some_field;
}
Of course the API can change a bit to make it index safe with Option, and maybe with different types of mutability settings. But I think this is something a lot of users can benefit from.