Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
remove tag from title
Link
Herohtar
  • 5.7k
  • 4
  • 34
  • 44

Rust: Use mutable iterator twice

Source Link
Dekakaruk
  • 767
  • 1
  • 8
  • 14

Rust: Use mutable iterator twice

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?