-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Description
-
I have searched open and closed issues and pull requests for duplicates, using these search terms:
-
I have checked the latest
mainbranch to see if this has already been fixed, in this file:
URL to the section(s) of the book with this problem:
https://doc.rust-lang.org/book/ch19-02-refutability.html#listing-19-9
Description of the problem:
Book says, quote: "If we have a refutable pattern where an irrefutable pattern is needed, we can fix it by changing the code that uses the pattern: instead of using let, we can use let else. Then, if the pattern doesn’t match, the code will just skip the code in the curly brackets, giving it a way to continue validly. Listing 19-9 shows how to fix the code in Listing 19-8."
And then it proceeds to show Listing 19-9 as shown below:
fn main() { let some_option_value: Option<i32> = None; let Some(x) = some_option_value else { return; }; }
THe above Listing 19-9 makes this statement inaccurate: "Then, if the pattern doesn’t match, the code will just skip the code in the curly brackets, giving it a way to continue validly. "
This is because is the pattern doesn't match, it executes the code in the curly brackets ( return; ), which exits the function entirely. It doesn't "skip" the curly brackets - it runs them and returns.
Suggested fix:
The book should have said: "if the pattern doesn't match, the code will execute the else block (which must diverge - return, panic, etc.), preventing invalid execution from continuing."