I'm trying to write a general function that takes an iterable (or iterator) and iterates it twice, at least once mutably, like:
fn f(iter: I) where I: Iterator<Item = &mut i32> + Clone { for i in iter.clone() { println!("{}", *i); } for i in iter.clone() { *i += 1; } } But it doesn't work because mutable iterators tend not to have clone() implemented, and for just reasons. My real world example is iteration over HashMap values, where std::collections::hash_map::ValuesMut is not Clone. Are there any ways to do it?
&mut HashMap<K, V>orValuesMut<'_, K, V>and use it more than once to create an iterator.