@@ -414,6 +414,64 @@ pub trait DoubleEndedIterator: Iterator {
414
414
/// ```
415
415
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
416
416
fn next_back ( & mut self ) -> Option < Self :: Item > ;
417
+
418
+ /// Searches for an element of an iterator from the right that satisfies a predicate.
419
+ ///
420
+ /// `rfind()` takes a closure that returns `true` or `false`. It applies
421
+ /// this closure to each element of the iterator, starting at the end, and if any
422
+ /// of them return `true`, then `rfind()` returns [`Some(element)`]. If they all return
423
+ /// `false`, it returns [`None`].
424
+ ///
425
+ /// `rfind()` is short-circuiting; in other words, it will stop processing
426
+ /// as soon as the closure returns `true`.
427
+ ///
428
+ /// Because `rfind()` takes a reference, and many iterators iterate over
429
+ /// references, this leads to a possibly confusing situation where the
430
+ /// argument is a double reference. You can see this effect in the
431
+ /// examples below, with `&&x`.
432
+ ///
433
+ /// [`Some(element)`]: ../../std/option/enum.Option.html#variant.Some
434
+ /// [`None`]: ../../std/option/enum.Option.html#variant.None
435
+ ///
436
+ /// # Examples
437
+ ///
438
+ /// Basic usage:
439
+ ///
440
+ /// ```
441
+ /// #![feature(iter_rfind)]
442
+ ///
443
+ /// let a = [1, 2, 3];
444
+ ///
445
+ /// assert_eq!(a.iter().rfind(|&&x| x == 2), Some(&2));
446
+ ///
447
+ /// assert_eq!(a.iter().rfind(|&&x| x == 5), None);
448
+ /// ```
449
+ ///
450
+ /// Stopping at the first `true`:
451
+ ///
452
+ /// ```
453
+ /// #![feature(iter_rfind)]
454
+ ///
455
+ /// let a = [1, 2, 3];
456
+ ///
457
+ /// let mut iter = a.iter();
458
+ ///
459
+ /// assert_eq!(iter.rfind(|&&x| x == 2), Some(&2));
460
+ ///
461
+ /// // we can still use `iter`, as there are more elements.
462
+ /// assert_eq!(iter.next_back(), Some(&1));
463
+ /// ```
464
+ #[ inline]
465
+ #[ unstable( feature = "iter_rfind" , issue = "39480" ) ]
466
+ fn rfind < P > ( & mut self , mut predicate : P ) -> Option < Self :: Item > where
467
+ Self : Sized ,
468
+ P : FnMut ( & Self :: Item ) -> bool
469
+ {
470
+ for x in self . by_ref ( ) . rev ( ) {
471
+ if predicate ( & x) { return Some ( x) }
472
+ }
473
+ None
474
+ }
417
475
}
418
476
419
477
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
0 commit comments