Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
khs26 opened this issue Dec 25, 2024 · 0 comments
Open

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

khs26 opened this issue Dec 25, 2024 · 0 comments

Comments

@khs26
Copy link

khs26 commented Dec 25, 2024

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...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant