Skip to content

Suggestion: 3.4 IntoIter - motivate the use of a newtype struct #311

@khs26

Description

@khs26

Thanks for the excellent tutorial.

Sadly, Rust has nothing like a yield statement (yet), so we're going to have to implement the logic ourselves. Also, there's actually 3 different kinds of iterator each collection should endeavour to implement:

  • IntoIter - T
  • IterMut - &mut T
  • Iter - &T

We actually already have all the tools to implement IntoIter using List's interface: just call pop over and over. As such, we'll just implement IntoIter as a newtype wrapper around List

I think could do with a bit more ramp up into why you use a newtype wrapper. I'm not sure what the best way to approach it is, but maybe the following questions/comments could help:

  • Mention that we could just use self.pop(), but that the other iterator types will need to keep track of where they got to in the list.
  • What is a newtype struct?
  • When do we use a newtype struct?
    • Maybe gloss over the traits on foreign types stuff...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions