Skip to content

Commit 3fcf84a

Browse files
committed
clarify that ExactSizeIterator::len returns the remaining length
1 parent 7425fb2 commit 3fcf84a

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

library/core/src/iter/traits/exact_size.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@
6666
///
6767
/// // And now we can use it!
6868
///
69-
/// let counter = Counter::new();
69+
/// let mut counter = Counter::new();
7070
///
7171
/// assert_eq!(5, counter.len());
72+
/// let _ = counter.next();
73+
/// assert_eq!(4, counter.len());
7274
/// ```
7375
#[stable(feature = "rust1", since = "1.0.0")]
7476
pub trait ExactSizeIterator: Iterator {
75-
/// Returns the exact length of the iterator.
77+
/// Returns the exact remaining length of the iterator.
7678
///
7779
/// The implementation ensures that the iterator will return exactly `len()`
7880
/// more times a [`Some(T)`] value, before returning [`None`].
@@ -93,9 +95,11 @@ pub trait ExactSizeIterator: Iterator {
9395
///
9496
/// ```
9597
/// // a finite range knows exactly how many times it will iterate
96-
/// let five = 0..5;
98+
/// let mut range = 0..5;
9799
///
98-
/// assert_eq!(5, five.len());
100+
/// assert_eq!(5, range.len());
101+
/// let _ = range.next();
102+
/// assert_eq!(4, range.len());
99103
/// ```
100104
#[inline]
101105
#[stable(feature = "rust1", since = "1.0.0")]

library/core/src/iter/traits/iterator.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,11 @@ pub trait Iterator {
177177
///
178178
/// ```
179179
/// let a = [1, 2, 3];
180-
/// let iter = a.iter();
180+
/// let mut iter = a.iter();
181181
///
182182
/// assert_eq!((3, Some(3)), iter.size_hint());
183+
/// let _ = iter.next();
184+
/// assert_eq!((2, Some(2)), iter.size_hint());
183185
/// ```
184186
///
185187
/// A more complex example:

0 commit comments

Comments
 (0)