Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing code examples on Iterator trait #64174

Merged
merged 1 commit into from
Sep 6, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/libcore/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2546,6 +2546,16 @@ pub trait Iterator {

/// Lexicographically compares the elements of this `Iterator` with those
/// of another.
///
/// # Examples
///
/// ```
/// use std::cmp::Ordering;
///
/// assert_eq!([1].iter().cmp([1].iter()), Ordering::Equal);
/// assert_eq!([1].iter().cmp([1, 2].iter()), Ordering::Less);
/// assert_eq!([1, 2].iter().cmp([1].iter()), Ordering::Greater);
/// ```
#[stable(feature = "iter_order", since = "1.5.0")]
fn cmp<I>(mut self, other: I) -> Ordering where
I: IntoIterator<Item = Self::Item>,
Expand Down Expand Up @@ -2578,6 +2588,18 @@ pub trait Iterator {

/// Lexicographically compares the elements of this `Iterator` with those
/// of another.
///
/// # Examples
///
/// ```
/// use std::cmp::Ordering;
///
/// assert_eq!([1.].iter().partial_cmp([1.].iter()), Some(Ordering::Equal));
/// assert_eq!([1.].iter().partial_cmp([1., 2.].iter()), Some(Ordering::Less));
/// assert_eq!([1., 2.].iter().partial_cmp([1.].iter()), Some(Ordering::Greater));
///
/// assert_eq!([std::f64::NAN].iter().partial_cmp([1.].iter()), None);
/// ```
#[stable(feature = "iter_order", since = "1.5.0")]
fn partial_cmp<I>(mut self, other: I) -> Option<Ordering> where
I: IntoIterator,
Expand Down Expand Up @@ -2610,6 +2632,13 @@ pub trait Iterator {

/// Determines if the elements of this `Iterator` are equal to those of
/// another.
///
/// # Examples
///
/// ```
/// assert_eq!([1].iter().eq([1].iter()), true);
/// assert_eq!([1].iter().eq([1, 2].iter()), false);
/// ```
#[stable(feature = "iter_order", since = "1.5.0")]
fn eq<I>(mut self, other: I) -> bool where
I: IntoIterator,
Expand All @@ -2635,6 +2664,13 @@ pub trait Iterator {

/// Determines if the elements of this `Iterator` are unequal to those of
/// another.
///
/// # Examples
///
/// ```
/// assert_eq!([1].iter().ne([1].iter()), false);
/// assert_eq!([1].iter().ne([1, 2].iter()), true);
/// ```
#[stable(feature = "iter_order", since = "1.5.0")]
fn ne<I>(self, other: I) -> bool where
I: IntoIterator,
Expand All @@ -2646,6 +2682,14 @@ pub trait Iterator {

/// Determines if the elements of this `Iterator` are lexicographically
/// less than those of another.
///
/// # Examples
///
/// ```
/// assert_eq!([1].iter().lt([1].iter()), false);
/// assert_eq!([1].iter().lt([1, 2].iter()), true);
/// assert_eq!([1, 2].iter().lt([1].iter()), false);
/// ```
#[stable(feature = "iter_order", since = "1.5.0")]
fn lt<I>(self, other: I) -> bool where
I: IntoIterator,
Expand All @@ -2657,6 +2701,14 @@ pub trait Iterator {

/// Determines if the elements of this `Iterator` are lexicographically
/// less or equal to those of another.
///
/// # Examples
///
/// ```
/// assert_eq!([1].iter().le([1].iter()), true);
/// assert_eq!([1].iter().le([1, 2].iter()), true);
/// assert_eq!([1, 2].iter().le([1].iter()), false);
/// ```
#[stable(feature = "iter_order", since = "1.5.0")]
fn le<I>(self, other: I) -> bool where
I: IntoIterator,
Expand All @@ -2671,6 +2723,14 @@ pub trait Iterator {

/// Determines if the elements of this `Iterator` are lexicographically
/// greater than those of another.
///
/// # Examples
///
/// ```
/// assert_eq!([1].iter().gt([1].iter()), false);
/// assert_eq!([1].iter().gt([1, 2].iter()), false);
/// assert_eq!([1, 2].iter().gt([1].iter()), true);
/// ```
#[stable(feature = "iter_order", since = "1.5.0")]
fn gt<I>(self, other: I) -> bool where
I: IntoIterator,
Expand All @@ -2682,6 +2742,14 @@ pub trait Iterator {

/// Determines if the elements of this `Iterator` are lexicographically
/// greater than or equal to those of another.
///
/// # Examples
///
/// ```
/// assert_eq!([1].iter().ge([1].iter()), true);
/// assert_eq!([1].iter().ge([1, 2].iter()), false);
/// assert_eq!([1, 2].iter().ge([1].iter()), true);
/// ```
#[stable(feature = "iter_order", since = "1.5.0")]
fn ge<I>(self, other: I) -> bool where
I: IntoIterator,
Expand Down Expand Up @@ -2730,6 +2798,18 @@ pub trait Iterator {
/// function to determine the ordering of two elements. Apart from that, it's equivalent to
/// [`is_sorted`]; see its documentation for more information.
///
/// # Examples
///
/// ```
/// #![feature(is_sorted)]
///
/// assert!([1, 2, 2, 9].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
/// assert!(![1, 3, 2, 4].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
/// assert!([0].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
/// assert!(std::iter::empty::<i32>().is_sorted_by(|a, b| a.partial_cmp(b)));
/// assert!(![0.0, 1.0, std::f32::NAN].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
/// ```
///
/// [`is_sorted`]: trait.Iterator.html#method.is_sorted
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
fn is_sorted_by<F>(mut self, mut compare: F) -> bool
Expand Down