Skip to content

Commit f88cf51

Browse files
Tail: skip the starting part of iterator
If the iterator is exact sized, then `.collect()` finishes the work. Thanks to scottmcm!
1 parent 4c9db0e commit f88cf51

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3166,7 +3166,9 @@ pub trait Itertools: Iterator {
31663166
}
31673167
1 => self.last().into_iter().collect(),
31683168
_ => {
3169-
let mut iter = self.fuse();
3169+
// Skip the starting part of iterator if possible.
3170+
let (low, _) = self.size_hint();
3171+
let mut iter = self.fuse().skip(low.saturating_sub(n));
31703172
let mut data: Vec<_> = iter.by_ref().take(n).collect();
31713173
// Update `data` cyclically.
31723174
let idx = iter.fold(0, |i, val| {

0 commit comments

Comments
 (0)