Skip to content

Commit a852ebb

Browse files
authored
Rollup merge of #64174 - GuillaumeGomez:missing-iterator-examples, r=sfackler
Add missing code examples on Iterator trait Fixes #63865 cc @rust-lang/docs
2 parents 6968e53 + c9bd2f7 commit a852ebb

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

src/libcore/iter/traits/iterator.rs

+80
Original file line numberDiff line numberDiff line change
@@ -2546,6 +2546,16 @@ pub trait Iterator {
25462546

25472547
/// Lexicographically compares the elements of this `Iterator` with those
25482548
/// of another.
2549+
///
2550+
/// # Examples
2551+
///
2552+
/// ```
2553+
/// use std::cmp::Ordering;
2554+
///
2555+
/// assert_eq!([1].iter().cmp([1].iter()), Ordering::Equal);
2556+
/// assert_eq!([1].iter().cmp([1, 2].iter()), Ordering::Less);
2557+
/// assert_eq!([1, 2].iter().cmp([1].iter()), Ordering::Greater);
2558+
/// ```
25492559
#[stable(feature = "iter_order", since = "1.5.0")]
25502560
fn cmp<I>(mut self, other: I) -> Ordering where
25512561
I: IntoIterator<Item = Self::Item>,
@@ -2578,6 +2588,18 @@ pub trait Iterator {
25782588

25792589
/// Lexicographically compares the elements of this `Iterator` with those
25802590
/// of another.
2591+
///
2592+
/// # Examples
2593+
///
2594+
/// ```
2595+
/// use std::cmp::Ordering;
2596+
///
2597+
/// assert_eq!([1.].iter().partial_cmp([1.].iter()), Some(Ordering::Equal));
2598+
/// assert_eq!([1.].iter().partial_cmp([1., 2.].iter()), Some(Ordering::Less));
2599+
/// assert_eq!([1., 2.].iter().partial_cmp([1.].iter()), Some(Ordering::Greater));
2600+
///
2601+
/// assert_eq!([std::f64::NAN].iter().partial_cmp([1.].iter()), None);
2602+
/// ```
25812603
#[stable(feature = "iter_order", since = "1.5.0")]
25822604
fn partial_cmp<I>(mut self, other: I) -> Option<Ordering> where
25832605
I: IntoIterator,
@@ -2610,6 +2632,13 @@ pub trait Iterator {
26102632

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

26362665
/// Determines if the elements of this `Iterator` are unequal to those of
26372666
/// another.
2667+
///
2668+
/// # Examples
2669+
///
2670+
/// ```
2671+
/// assert_eq!([1].iter().ne([1].iter()), false);
2672+
/// assert_eq!([1].iter().ne([1, 2].iter()), true);
2673+
/// ```
26382674
#[stable(feature = "iter_order", since = "1.5.0")]
26392675
fn ne<I>(self, other: I) -> bool where
26402676
I: IntoIterator,
@@ -2646,6 +2682,14 @@ pub trait Iterator {
26462682

26472683
/// Determines if the elements of this `Iterator` are lexicographically
26482684
/// less than those of another.
2685+
///
2686+
/// # Examples
2687+
///
2688+
/// ```
2689+
/// assert_eq!([1].iter().lt([1].iter()), false);
2690+
/// assert_eq!([1].iter().lt([1, 2].iter()), true);
2691+
/// assert_eq!([1, 2].iter().lt([1].iter()), false);
2692+
/// ```
26492693
#[stable(feature = "iter_order", since = "1.5.0")]
26502694
fn lt<I>(self, other: I) -> bool where
26512695
I: IntoIterator,
@@ -2657,6 +2701,14 @@ pub trait Iterator {
26572701

26582702
/// Determines if the elements of this `Iterator` are lexicographically
26592703
/// less or equal to those of another.
2704+
///
2705+
/// # Examples
2706+
///
2707+
/// ```
2708+
/// assert_eq!([1].iter().le([1].iter()), true);
2709+
/// assert_eq!([1].iter().le([1, 2].iter()), true);
2710+
/// assert_eq!([1, 2].iter().le([1].iter()), false);
2711+
/// ```
26602712
#[stable(feature = "iter_order", since = "1.5.0")]
26612713
fn le<I>(self, other: I) -> bool where
26622714
I: IntoIterator,
@@ -2671,6 +2723,14 @@ pub trait Iterator {
26712723

26722724
/// Determines if the elements of this `Iterator` are lexicographically
26732725
/// greater than those of another.
2726+
///
2727+
/// # Examples
2728+
///
2729+
/// ```
2730+
/// assert_eq!([1].iter().gt([1].iter()), false);
2731+
/// assert_eq!([1].iter().gt([1, 2].iter()), false);
2732+
/// assert_eq!([1, 2].iter().gt([1].iter()), true);
2733+
/// ```
26742734
#[stable(feature = "iter_order", since = "1.5.0")]
26752735
fn gt<I>(self, other: I) -> bool where
26762736
I: IntoIterator,
@@ -2682,6 +2742,14 @@ pub trait Iterator {
26822742

26832743
/// Determines if the elements of this `Iterator` are lexicographically
26842744
/// greater than or equal to those of another.
2745+
///
2746+
/// # Examples
2747+
///
2748+
/// ```
2749+
/// assert_eq!([1].iter().ge([1].iter()), true);
2750+
/// assert_eq!([1].iter().ge([1, 2].iter()), false);
2751+
/// assert_eq!([1, 2].iter().ge([1].iter()), true);
2752+
/// ```
26852753
#[stable(feature = "iter_order", since = "1.5.0")]
26862754
fn ge<I>(self, other: I) -> bool where
26872755
I: IntoIterator,
@@ -2730,6 +2798,18 @@ pub trait Iterator {
27302798
/// function to determine the ordering of two elements. Apart from that, it's equivalent to
27312799
/// [`is_sorted`]; see its documentation for more information.
27322800
///
2801+
/// # Examples
2802+
///
2803+
/// ```
2804+
/// #![feature(is_sorted)]
2805+
///
2806+
/// assert!([1, 2, 2, 9].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
2807+
/// assert!(![1, 3, 2, 4].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
2808+
/// assert!([0].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
2809+
/// assert!(std::iter::empty::<i32>().is_sorted_by(|a, b| a.partial_cmp(b)));
2810+
/// assert!(![0.0, 1.0, std::f32::NAN].iter().is_sorted_by(|a, b| a.partial_cmp(b)));
2811+
/// ```
2812+
///
27332813
/// [`is_sorted`]: trait.Iterator.html#method.is_sorted
27342814
#[unstable(feature = "is_sorted", reason = "new API", issue = "53485")]
27352815
fn is_sorted_by<F>(mut self, mut compare: F) -> bool

0 commit comments

Comments
 (0)