]> git.lizzy.rs Git - rust.git/commitdiff
Fix doc error for ExactSizeIterator
authorManuel <LifeIsHealthy@icloud.com>
Tue, 21 Mar 2017 21:18:52 +0000 (22:18 +0100)
committerGitHub <noreply@github.com>
Tue, 21 Mar 2017 21:18:52 +0000 (22:18 +0100)
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.

src/libcore/iter/traits.rs

index 3415b0eea9bd01519090bdd9c20567e906eb2819..34f14ef53f8935e890f6459472aa02c971cbf677 100644 (file)
@@ -536,9 +536,9 @@ fn next_back(&mut self) -> Option<I::Item> { (**self).next_back() }
 /// #     }
 /// # }
 /// impl ExactSizeIterator for Counter {
-///     // We already have the number of iterations, so we can use it directly.
+///     // We can easily calculate the remaining number of iterations.
 ///     fn len(&self) -> usize {
-///         self.count
+///         5 - self.count
 ///     }
 /// }
 ///
@@ -546,7 +546,7 @@ fn next_back(&mut self) -> Option<I::Item> { (**self).next_back() }
 ///
 /// let counter = Counter::new();
 ///
-/// assert_eq!(0, counter.len());
+/// assert_eq!(5, counter.len());
 /// ```
 #[stable(feature = "rust1", since = "1.0.0")]
 pub trait ExactSizeIterator: Iterator {