Skip to content

Commit a8800bb

Browse files
authored
Fix doc error for ExactSizeIterator
The code example in the trait documentation of ExactSizeIterator has an incorrect implementation of the len method that does not return the number of times the example iterator 'Counter' will iterate. This may confuse readers of the docs as the example code will compile but doesn't uphold the trait's contract. This is easily fixed by modifying the implementation of len and changing the assert statement to actually assert the correct behaviour. I also slightly modified a code comment to better reflect what the method returns.
1 parent 58c701f commit a8800bb

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/libcore/iter/traits.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -536,17 +536,17 @@ impl<'a, I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for &'a mut I {
536536
/// # }
537537
/// # }
538538
/// impl ExactSizeIterator for Counter {
539-
/// // We already have the number of iterations, so we can use it directly.
539+
/// // We can easily calculate the remaining number of iterations.
540540
/// fn len(&self) -> usize {
541-
/// self.count
541+
/// 5 - self.count
542542
/// }
543543
/// }
544544
///
545545
/// // And now we can use it!
546546
///
547547
/// let counter = Counter::new();
548548
///
549-
/// assert_eq!(0, counter.len());
549+
/// assert_eq!(5, counter.len());
550550
/// ```
551551
#[stable(feature = "rust1", since = "1.0.0")]
552552
pub trait ExactSizeIterator: Iterator {

0 commit comments

Comments
 (0)