Skip to content

Commit 5850f0b

Browse files
committed
Fix off-by-ones
1 parent 37771d4 commit 5850f0b

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/libcore/iter/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -698,13 +698,16 @@ impl<I> Iterator for StepBy<I> where I: Iterator {
698698
#[inline]
699699
fn nth(&mut self, mut n: usize) -> Option<Self::Item> {
700700
if self.first_take {
701+
self.first_take = false;
702+
let first = self.iter.next();
701703
if n == 0 {
702-
self.first_take = false;
703-
return self.iter.next()
704+
return first;
704705
}
705706
n -= 1;
706707
}
707-
self.iter.nth(n * self.step)
708+
// n and self.step are indices, thus we need to add 1 before multiplying.
709+
// After that we need to subtract 1 from the result to convert it back to an index.
710+
self.iter.nth((n + 1) * (self.step + 1) - 1)
708711
}
709712
}
710713

0 commit comments

Comments
 (0)