#[inline]
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
+ #[inline]
+ fn nth(&mut self, n: usize) -> Option<<I as Iterator>::Item> { self.iter.nth_back(n) }
+
fn try_fold<B, F, R>(&mut self, init: B, f: F) -> R where
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
{
#[inline]
fn next_back(&mut self) -> Option<<I as Iterator>::Item> { self.iter.next() }
+ #[inline]
+ fn nth_back(&mut self, n: usize) -> Option<<I as Iterator>::Item> { self.iter.nth(n) }
+
fn try_rfold<B, F, R>(&mut self, init: B, f: F) -> R where
Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
{
#[stable(feature = "rust1", since = "1.0.0")]
fn next_back(&mut self) -> Option<Self::Item>;
+ /// Returns the `n`th element from the end of the iterator.
+ ///
+ /// This is essentially the reversed version of [`nth`]. Although like most indexing
+ /// operations, the count starts from zero, so `nth_back(0)` returns the first value fro
+ /// the end, `nth_back(1)` the second, and so on.
+ ///
+ /// Note that all elements between the end and the returned element will be
+ /// consumed, including the returned element. This also means that calling
+ /// `nth_back(0)` multiple times on the same iterator will return different
+ /// elements.
+ ///
+ /// `nth_back()` will return [`None`] if `n` is greater than or equal to the length of the
+ /// iterator.
+ ///
+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
+ /// [`nth`]: ../../std/iter/trait.Iterator.html#method.nth
+ ///
+ /// # Examples
+ ///
+ /// Basic usage:
+ ///
+ /// ```
+ /// #![feature(iter_nth_back)]
+ /// let a = [1, 2, 3];
+ /// assert_eq!(a.iter().nth_back(2), Some(&1));
+ /// ```
+ ///
+ /// Calling `nth_back()` multiple times doesn't rewind the iterator:
+ ///
+ /// ```
+ /// #![feature(iter_nth_back)]
+ /// let a = [1, 2, 3];
+ ///
+ /// let mut iter = a.iter();
+ ///
+ /// assert_eq!(iter.nth_back(1), Some(&2));
+ /// assert_eq!(iter.nth_back(1), None);
+ /// ```
+ ///
+ /// Returning `None` if there are less than `n + 1` elements:
+ ///
+ /// ```
+ /// #![feature(iter_nth_back)]
+ /// let a = [1, 2, 3];
+ /// assert_eq!(a.iter().nth_back(10), None);
+ /// ```
+ #[inline]
+ #[unstable(feature = "iter_nth_back", issue = "56995")]
+ fn nth_back(&mut self, mut n: usize) -> Option<Self::Item> {
+ for x in self.rev() {
+ if n == 0 { return Some(x) }
+ n -= 1;
+ }
+ None
+ }
+
/// This is the reverse version of [`try_fold()`]: it takes elements
/// starting from the back of the iterator.
///
/// ```
#[inline]
#[stable(feature = "iterator_try_fold", since = "1.27.0")]
- fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R where
- Self: Sized, F: FnMut(B, Self::Item) -> R, R: Try<Ok=B>
+ fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R
+ where
+ Self: Sized,
+ F: FnMut(B, Self::Item) -> R,
+ R: Try<Ok=B>
{
let mut accum = init;
while let Some(x) = self.next_back() {
/// ```
#[inline]
#[stable(feature = "iter_rfold", since = "1.27.0")]
- fn rfold<B, F>(mut self, accum: B, mut f: F) -> B where
- Self: Sized, F: FnMut(B, Self::Item) -> B,
+ fn rfold<B, F>(mut self, accum: B, mut f: F) -> B
+ where
+ Self: Sized,
+ F: FnMut(B, Self::Item) -> B,
{
self.try_rfold(accum, move |acc, x| Ok::<B, !>(f(acc, x))).unwrap()
}
/// ```
#[inline]
#[stable(feature = "iter_rfind", since = "1.27.0")]
- fn rfind<P>(&mut self, mut predicate: P) -> Option<Self::Item> where
+ fn rfind<P>(&mut self, mut predicate: P) -> Option<Self::Item>
+ where
Self: Sized,
P: FnMut(&Self::Item) -> bool
{
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for &'a mut I {
- fn next_back(&mut self) -> Option<I::Item> { (**self).next_back() }
+ fn next_back(&mut self) -> Option<I::Item> {
+ (**self).next_back()
+ }
+ fn nth_back(&mut self, n: usize) -> Option<I::Item> {
+ (**self).nth_back(n)
+ }
}
/// An iterator that knows its exact length.
assert_eq!(v.iter().nth(v.len()), None);
}
+#[test]
+fn test_iterator_nth_back() {
+ let v: &[_] = &[0, 1, 2, 3, 4];
+ for i in 0..v.len() {
+ assert_eq!(v.iter().nth_back(i).unwrap(), &v[v.len() - 1 - i]);
+ }
+ assert_eq!(v.iter().nth_back(v.len()), None);
+}
+
+#[test]
+fn test_iterator_rev_nth_back() {
+ let v: &[_] = &[0, 1, 2, 3, 4];
+ for i in 0..v.len() {
+ assert_eq!(v.iter().rev().nth_back(i).unwrap(), &v[i]);
+ }
+ assert_eq!(v.iter().rev().nth_back(v.len()), None);
+}
+
+#[test]
+fn test_iterator_rev_nth() {
+ let v: &[_] = &[0, 1, 2, 3, 4];
+ for i in 0..v.len() {
+ assert_eq!(v.iter().rev().nth(i).unwrap(), &v[v.len() - 1 - i]);
+ }
+ assert_eq!(v.iter().rev().nth(v.len()), None);
+}
+
#[test]
fn test_iterator_last() {
let v: &[_] = &[0, 1, 2, 3, 4];