We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2963a6f commit 306f0d0Copy full SHA for 306f0d0
src/libcore/iter.rs
@@ -2039,14 +2039,17 @@ impl<I> Iterator for Skip<I> where I: Iterator {
2039
2040
#[inline]
2041
fn nth(&mut self, n: usize) -> Option<I::Item> {
2042
+ // Can't just add n + self.n due to overflow.
2043
if self.n == 0 {
2044
self.iter.nth(n)
- } else if n == 0 {
2045
- self.next()
2046
} else {
2047
- self.next();
2048
- // Recurse on the first case.
2049
- self.iter.nth(n-1)
+ let to_skip = self.n;
+ self.n = 0;
+ // nth(n) skips n+1
+ if self.iter.nth(to_skip-1).is_none() {
2050
+ return None;
2051
+ }
2052
+ self.iter.nth(n)
2053
}
2054
2055
0 commit comments