Skip to content

Commit

Permalink
Merge pull request #1425 from Stromberg90/patch-1
Browse files Browse the repository at this point in the history
Changed impl to use Self::Item
  • Loading branch information
marioidival authored Mar 22, 2021
2 parents 717df18 + 8001357 commit 08fc1aa
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/trait/iter.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ struct Fibonacci {
// Implement `Iterator` for `Fibonacci`.
// The `Iterator` trait only requires a method to be defined for the `next` element.
impl Iterator for Fibonacci {
// We can refer to this type using Self::Item
type Item = u32;
// Here, we define the sequence using `.curr` and `.next`.
// The return type is `Option<T>`:
// * When the `Iterator` is finished, `None` is returned.
// * Otherwise, the next value is wrapped in `Some` and returned.
fn next(&mut self) -> Option<u32> {
// We use Self::Item in the return type, so we can change
// the type without having to update the function signatures.
fn next(&mut self) -> Option<Self::Item> {
let new_next = self.curr + self.next;
self.curr = self.next;
Expand Down

0 comments on commit 08fc1aa

Please sign in to comment.