implement nth_back for Enumerate
This commit is contained in:
parent
fae2a68ba2
commit
2605537012
|
@ -980,6 +980,16 @@ impl<I> DoubleEndedIterator for Enumerate<I> where
|
|||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn nth_back(&mut self, n: usize) -> Option<(usize, <I as Iterator>::Item)> {
|
||||
self.iter.nth_back(n).map(|a| {
|
||||
let len = self.iter.len();
|
||||
// Can safely add, `ExactSizeIterator` promises that the number of
|
||||
// elements fits into a `usize`.
|
||||
(self.count + len, a)
|
||||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn try_rfold<Acc, Fold, R>(&mut self, init: Acc, mut fold: Fold) -> R where
|
||||
Self: Sized, Fold: FnMut(Acc, Self::Item) -> R, R: Try<Ok=Acc>
|
||||
|
|
|
@ -389,6 +389,24 @@ fn test_iterator_enumerate_nth() {
|
|||
assert_eq!(i, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterator_enumerate_nth_back() {
|
||||
let xs = [0, 1, 2, 3, 4, 5];
|
||||
let mut it = xs.iter().enumerate();
|
||||
while let Some((i, &x)) = it.nth_back(0) {
|
||||
assert_eq!(i, x);
|
||||
}
|
||||
|
||||
let mut it = xs.iter().enumerate();
|
||||
while let Some((i, &x)) = it.nth_back(1) {
|
||||
assert_eq!(i, x);
|
||||
}
|
||||
|
||||
let (i, &x) = xs.iter().enumerate().nth_back(3).unwrap();
|
||||
assert_eq!(i, x);
|
||||
assert_eq!(i, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterator_enumerate_count() {
|
||||
let xs = [0, 1, 2, 3, 4, 5];
|
||||
|
|
Loading…
Reference in New Issue