Skip to content

Commit a7405db

Browse files
Rollup merge of #39955 - mp4096:master, r=GuillaumeGomez
Docs: Better explanation of return values for min, max functions for the Iterator trait Added an explanation that `None` is returned if an iterator is empty. Also added examples for `max` and `min`. I chose not to add examples for other functions like `max_by_key` etc. so that the examples stay concised and focused on the main functionality.
2 parents eab60d6 + eee6752 commit a7405db

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/libcore/iter/iterator.rs

+22-6
Original file line numberDiff line numberDiff line change
@@ -1616,16 +1616,20 @@ pub trait Iterator {
16161616
/// Returns the maximum element of an iterator.
16171617
///
16181618
/// If several elements are equally maximum, the last element is
1619-
/// returned.
1619+
/// returned. If the iterator is empty, [`None`] is returned.
1620+
///
1621+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
16201622
///
16211623
/// # Examples
16221624
///
16231625
/// Basic usage:
16241626
///
16251627
/// ```
16261628
/// let a = [1, 2, 3];
1629+
/// let b: Vec<u32> = Vec::new();
16271630
///
16281631
/// assert_eq!(a.iter().max(), Some(&3));
1632+
/// assert_eq!(b.iter().max(), None);
16291633
/// ```
16301634
#[inline]
16311635
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1642,16 +1646,20 @@ pub trait Iterator {
16421646
/// Returns the minimum element of an iterator.
16431647
///
16441648
/// If several elements are equally minimum, the first element is
1645-
/// returned.
1649+
/// returned. If the iterator is empty, [`None`] is returned.
1650+
///
1651+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
16461652
///
16471653
/// # Examples
16481654
///
16491655
/// Basic usage:
16501656
///
16511657
/// ```
16521658
/// let a = [1, 2, 3];
1659+
/// let b: Vec<u32> = Vec::new();
16531660
///
16541661
/// assert_eq!(a.iter().min(), Some(&1));
1662+
/// assert_eq!(b.iter().min(), None);
16551663
/// ```
16561664
#[inline]
16571665
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1669,7 +1677,9 @@ pub trait Iterator {
16691677
/// specified function.
16701678
///
16711679
/// If several elements are equally maximum, the last element is
1672-
/// returned.
1680+
/// returned. If the iterator is empty, [`None`] is returned.
1681+
///
1682+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
16731683
///
16741684
/// # Examples
16751685
///
@@ -1694,7 +1704,9 @@ pub trait Iterator {
16941704
/// specified comparison function.
16951705
///
16961706
/// If several elements are equally maximum, the last element is
1697-
/// returned.
1707+
/// returned. If the iterator is empty, [`None`] is returned.
1708+
///
1709+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
16981710
///
16991711
/// # Examples
17001712
///
@@ -1719,7 +1731,9 @@ pub trait Iterator {
17191731
/// specified function.
17201732
///
17211733
/// If several elements are equally minimum, the first element is
1722-
/// returned.
1734+
/// returned. If the iterator is empty, [`None`] is returned.
1735+
///
1736+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
17231737
///
17241738
/// # Examples
17251739
///
@@ -1743,7 +1757,9 @@ pub trait Iterator {
17431757
/// specified comparison function.
17441758
///
17451759
/// If several elements are equally minimum, the first element is
1746-
/// returned.
1760+
/// returned. If the iterator is empty, [`None`] is returned.
1761+
///
1762+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
17471763
///
17481764
/// # Examples
17491765
///

0 commit comments

Comments
 (0)