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

Chapter 13 Section 2 Question Clarification/Issue #230

Open
bmarwritescode opened this issue Nov 21, 2024 · 0 comments
Open

Chapter 13 Section 2 Question Clarification/Issue #230

bmarwritescode opened this issue Nov 21, 2024 · 0 comments

Comments

@bmarwritescode
Copy link

URL to the section(s) of the book with this problem:

https://rust-book.cs.brown.edu/ch13-02-iterators.html

Description of the problem:

On question 2 of the final quiz in Chapter 13 section 2, the following two code snippets are said to be semantically equivalent:

while let Some(x) = iter.next() {
    f(x);
}
for x in iter {
    f(x);
}

However, this might not be the case (depending on how you define semantic equivalence). The second code snippet will fail to compile if iter is a type that does not implement the Iterator trait. The first one, however, will compile as long as that type defines some function next that returns an option. To be explicit, the first will compile but the second will not assuming iter is an instance of the following struct:

struct MyIter {}

impl MyIter {
    fn next(&self) -> Option<u32> {
        None
    }
}

Suggested fix:

Add in an explicit type and/or definition for iter.

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