Repository navigation
docs: clarify iterator return types in Chapter 13.3 - #4862
Open
saranshhalwai wants to merge 1 commit into
Open
saranshhalwai wants to merge 1 commit into
saranshhalwai wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Chapter 13.3 suggests returning an iterator from
search()instead of collecting the results into a vector. Sounds simple enough, except following that suggestion breaks the existingrun()implementation.I followed the instructions and promptly got screwed over by
E0308.The problem is that
run()uses anif/elseexpression to choose betweensearch()andsearch_case_insensitive(). Changing only one function leaves the branches returning different types. Changing both to returnimpl Iteratordoesn't help either, since Rust treats each opaque return type as distinct.There's also the lifetime issue introduced by capturing
queryin the returned iterator.Changes
if/elseexpression stops compiling.impl Iterator<Item = &'a str>don't necessarily return compatible types.query.This is a documentation-only change. No existing examples or functionality have been modified.
Motivation
The book explicitly encourages readers to try this optimization, but doesn't mention that doing so requires changes elsewhere in the project.
I ran into this while working through the book, and it took me a while to figure out why two seemingly identical iterator return types weren't compatible.
Figured the next person shouldn't have to discover this through a compiler error.
Fixes #4518