Skip to content

Commit 3cf485c

Browse files
author
Clar Charr
committed
Move rfind to DoubleEndedIterator, add tracking issue.
1 parent 4af76e8 commit 3cf485c

File tree

2 files changed

+58
-58
lines changed

2 files changed

+58
-58
lines changed

src/libcore/iter/iterator.rs

-58
Original file line numberDiff line numberDiff line change
@@ -1488,64 +1488,6 @@ pub trait Iterator {
14881488
None
14891489
}
14901490

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-
15491491
/// Searches for an element in an iterator, returning its index.
15501492
///
15511493
/// `position()` takes a closure that returns `true` or `false`. It applies

src/libcore/iter/traits.rs

+58
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,64 @@ pub trait DoubleEndedIterator: Iterator {
414414
/// ```
415415
#[stable(feature = "rust1", since = "1.0.0")]
416416
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+
}
417475
}
418476

419477
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)