@@ -1488,6 +1488,64 @@ pub trait Iterator {
1488
1488
None
1489
1489
}
1490
1490
1491
+ /// Searches for an element of an iterator from the right that satisfies a predicate.
1492
+ ///
1493
+ /// `rfind()` takes a closure that returns `true` or `false`. It applies
1494
+ /// this closure to each element of the iterator, starting at the end, and if any
1495
+ /// of them return `true`, then `rfind()` returns [`Some(element)`]. If they all return
1496
+ /// `false`, it returns [`None`].
1497
+ ///
1498
+ /// `rfind()` is short-circuiting; in other words, it will stop processing
1499
+ /// as soon as the closure returns `true`.
1500
+ ///
1501
+ /// Because `rfind()` takes a reference, and many iterators iterate over
1502
+ /// references, this leads to a possibly confusing situation where the
1503
+ /// argument is a double reference. You can see this effect in the
1504
+ /// examples below, with `&&x`.
1505
+ ///
1506
+ /// [`Some(element)`]: ../../std/option/enum.Option.html#variant.Some
1507
+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
1508
+ ///
1509
+ /// # Examples
1510
+ ///
1511
+ /// Basic usage:
1512
+ ///
1513
+ /// ```
1514
+ /// #![feature(iter_rfind)]
1515
+ ///
1516
+ /// let a = [1, 2, 3];
1517
+ ///
1518
+ /// assert_eq!(a.iter().rfind(|&&x| x == 2), Some(&2));
1519
+ ///
1520
+ /// assert_eq!(a.iter().rfind(|&&x| x == 5), None);
1521
+ /// ```
1522
+ ///
1523
+ /// Stopping at the first `true`:
1524
+ ///
1525
+ /// ```
1526
+ /// #![feature(iter_rfind)]
1527
+ ///
1528
+ /// let a = [1, 2, 3];
1529
+ ///
1530
+ /// let mut iter = a.iter();
1531
+ ///
1532
+ /// assert_eq!(iter.rfind(|&&x| x == 2), Some(&2));
1533
+ ///
1534
+ /// // we can still use `iter`, as there are more elements.
1535
+ /// assert_eq!(iter.next_back(), Some(&1));
1536
+ /// ```
1537
+ #[ inline]
1538
+ #[ unstable( feature = "iter_rfind" , issue = "0" ) ]
1539
+ fn rfind < P > ( & mut self , mut predicate : P ) -> Option < Self :: Item > where
1540
+ Self : Sized + DoubleEndedIterator ,
1541
+ P : FnMut ( & Self :: Item ) -> bool
1542
+ {
1543
+ for x in self . by_ref ( ) . rev ( ) {
1544
+ if predicate ( & x) { return Some ( x) }
1545
+ }
1546
+ None
1547
+ }
1548
+
1491
1549
/// Searches for an element in an iterator, returning its index.
1492
1550
///
1493
1551
/// `position()` takes a closure that returns `true` or `false`. It applies
0 commit comments