Skip to content

Commit

Permalink
Rollup merge of rust-lang#98729 - the8472:exactsize-docs, r=thomcc
Browse files Browse the repository at this point in the history
clarify that ExactSizeIterator::len returns the remaining length

fixes rust-lang#98721
  • Loading branch information
matthiaskrgr committed Jun 30, 2022
2 parents ab3d1c1 + 3fcf84a commit d07c4c1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 8 additions & 4 deletions library/core/src/iter/traits/exact_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@
///
/// // And now we can use it!
///
/// let counter = Counter::new();
/// let mut counter = Counter::new();
///
/// assert_eq!(5, counter.len());
/// let _ = counter.next();
/// assert_eq!(4, counter.len());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub trait ExactSizeIterator: Iterator {
/// Returns the exact length of the iterator.
/// Returns the exact remaining length of the iterator.
///
/// The implementation ensures that the iterator will return exactly `len()`
/// more times a [`Some(T)`] value, before returning [`None`].
Expand All @@ -93,9 +95,11 @@ pub trait ExactSizeIterator: Iterator {
///
/// ```
/// // a finite range knows exactly how many times it will iterate
/// let five = 0..5;
/// let mut range = 0..5;
///
/// assert_eq!(5, five.len());
/// assert_eq!(5, range.len());
/// let _ = range.next();
/// assert_eq!(4, range.len());
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
4 changes: 3 additions & 1 deletion library/core/src/iter/traits/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,11 @@ pub trait Iterator {
///
/// ```
/// let a = [1, 2, 3];
/// let iter = a.iter();
/// let mut iter = a.iter();
///
/// assert_eq!((3, Some(3)), iter.size_hint());
/// let _ = iter.next();
/// assert_eq!((2, Some(2)), iter.size_hint());
/// ```
///
/// A more complex example:
Expand Down

0 comments on commit d07c4c1

Please sign in to comment.