Skip to content

Commit 740a62a

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 6168c5c commit 740a62a

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
@@ -3164,7 +3164,9 @@ pub trait Itertools: Iterator {
31643164
}
31653165
1 => self.last().into_iter().collect(),
31663166
_ => {
3167-
let mut iter = self.fuse();
3167+
// Skip the starting part of iterator if possible.
3168+
let (low, _) = self.size_hint();
3169+
let mut iter = self.fuse().skip(low.saturating_sub(n));
31683170
let mut data: Vec<_> = iter.by_ref().take(n).collect();
31693171
// Update `data` cyclically.
31703172
let idx = iter.fold(0, |i, val| {

0 commit comments

Comments
 (0)