Skip to content

Instantly share code, notes, and snippets.

@orez-
Created April 17, 2023 04:45
Show Gist options
  • Save orez-/e0d6bdce4c33ad46a5aac13f6425f70b to your computer and use it in GitHub Desktop.
Save orez-/e0d6bdce4c33ad46a5aac13f6425f70b to your computer and use it in GitHub Desktop.
fn interleave<I>(its: Vec<I>) -> Interleave<I> {
Interleave { its: its.into_iter().collect() }
}
struct Interleave<I> {
its: std::collections::VecDeque<I>,
}
impl<I: Iterator> Iterator for Interleave<I> {
type Item = I::Item;
fn next(&mut self) -> Option<I::Item> {
loop {
let mut it = self.its.pop_front()?;
if let Some(elem) = it.next() {
self.its.push_back(it);
return Some(elem);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment