Clarify lifetimes in search
function in chapter 12.4
#4448
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.
This PR updates the
search
function signature in chapter 12.4 to explicitly declare two lifetimes: one for thequery
parameter and one for thecontents
parameter.Original code:
Updated code:
Why this change?
While the original function signature compiles correctly, it may be confusing to beginners. Using a single lifetime parameter can suggest that both
query
andcontents
must live for the same duration, even though the return value depends only oncontents
.According to the first lifetime elision rule, as written in Chapter 10:
To make this explicit, the revised version introduces two separate lifetimes —
'a
and'b
. This highlights that the input references are independent, and clearly shows that the return value is tied only tocontents
.This adjustment improves clarity and aligns more closely with how lifetimes are explained earlier in the book. It helps prevent misconceptions and builds a more accurate mental model for new readers.
Let me know if you'd like any revisions — happy to adjust!