Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Iterator::intersperse #75784

Closed
wants to merge 8 commits into from
Prev Previous commit
Next Next commit
Implement size_hint
  • Loading branch information
jonas-schievink committed Sep 11, 2020
commit 97039d81a71cb1722571eb50a5c3dc63b5fd36a6
10 changes: 10 additions & 0 deletions library/core/src/iter/adapters/intersperse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,14 @@ where
accum
})
}

fn size_hint(&self) -> (usize, Option<usize>) {
// We'll yield the whole `self.iter`, and just as many separators, as well as (if present)
// `self.peek`.
let (lo, hi) = self.iter.size_hint();
let has_peek = self.peek.is_some() as usize;
let lo = lo.saturating_add(lo).saturating_add(has_peek);
let hi = hi.map(|hi| hi.saturating_add(hi).saturating_add(has_peek));
(lo, hi)
}
}