Skip to content

Commit ae81241

Browse files
authored
Rollup merge of rust-lang#68597 - ollie27:skip_nth_last, r=Amanieu
Simplify `Skip::nth` and `Skip::last` implementations The main improvement is to make `last` no longer recursive.
2 parents 6c4f859 + 84f3356 commit ae81241

File tree

1 file changed

+7
-13
lines changed
  • src/libcore/iter/adapters

1 file changed

+7
-13
lines changed

src/libcore/iter/adapters/mod.rs

+7-13
Original file line numberDiff line numberDiff line change
@@ -1890,17 +1890,15 @@ where
18901890
#[inline]
18911891
fn nth(&mut self, n: usize) -> Option<I::Item> {
18921892
// Can't just add n + self.n due to overflow.
1893-
if self.n == 0 {
1894-
self.iter.nth(n)
1895-
} else {
1893+
if self.n > 0 {
18961894
let to_skip = self.n;
18971895
self.n = 0;
18981896
// nth(n) skips n+1
18991897
if self.iter.nth(to_skip - 1).is_none() {
19001898
return None;
19011899
}
1902-
self.iter.nth(n)
19031900
}
1901+
self.iter.nth(n)
19041902
}
19051903

19061904
#[inline]
@@ -1916,17 +1914,13 @@ where
19161914

19171915
#[inline]
19181916
fn last(mut self) -> Option<I::Item> {
1919-
if self.n == 0 {
1920-
self.iter.last()
1921-
} else {
1922-
let next = self.next();
1923-
if next.is_some() {
1924-
// recurse. n should be 0.
1925-
self.last().or(next)
1926-
} else {
1927-
None
1917+
if self.n > 0 {
1918+
// nth(n) skips n+1
1919+
if self.iter.nth(self.n - 1).is_none() {
1920+
return None;
19281921
}
19291922
}
1923+
self.iter.last()
19301924
}
19311925

19321926
#[inline]

0 commit comments

Comments
 (0)