book icon indicating copy to clipboard operation
book copied to clipboard

Chapter 2 : continue is confusing

Open lgiraldi opened this issue 2 years ago • 4 comments

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

Description of the problem:

As a beginner developer in Rust, the error handling is confusing. If parse is not able to turn the string into a number, then the match expression is equivalent to let guess: u32 = continue;, which does not make sense to me but seems to be valid in rust. Even more confusing, the following code compiles and runs

fn main() {
    let guess: u32 = 42;
    let mut counter: u32 = 0;
    let max_count = 3;

    loop {
        counter += 1;
        if counter > max_count {
            break;
        }

        println!("guess = {guess}");
        guess = continue;
        println!("unreachable");
    }
}

Note that guess is immutable, but guess = continue; is not a problem to rustc.

Suggested fix:

  • Explain properly what is happening with the continue keyword under the hood,
  • Proposes a new error handling technique (reusing the previous guess in case of error for instance).

lgiraldi avatar May 07 '23 22:05 lgiraldi

I'm also new to Rust, but I think is because continue has the Never type. This is the link to the chapter that explains this part: https://doc.rust-lang.org/book/ch19-04-advanced-types.html#the-never-type-that-never-returns It's pretty late in the book so, it's logical that you might find confusing, but other things must be explained first in order to understand everything and, especially, why continue has the Never type

kinire98 avatar May 11 '23 09:05 kinire98

Thank you very much for the pointers. Maybe we could add a link from ch2 to ch19-04 and add 1 or 2 sentences to explain the issue? Note that the converse is true, ch19-04 is referencing Listing 2-5.

lgiraldi avatar May 11 '23 15:05 lgiraldi

Maybe it could be a good idea, but I think that part of the book is more than closed. And if you think about it, Rust already introduces a lot of new concepts, I don't think it's a good idea complicating things more for beginners (in the language or in general) telling them you can assing continue to a variable. But if you are interested maybe make a PR, shouldn't be too difficult

kinire98 avatar May 11 '23 16:05 kinire98

I agree, I've read the book several times and interlinking is missing so any links between the information in chapters or the text and the docs are a great improvement. I've read 80% of Python's documentation and I've never felt so lost as in Rust docs/book.

1oglop1 avatar Sep 09 '23 10:09 1oglop1

Thanks for the write-up. I can see how that confused you! If anyone else happens along and is confused by this, the use of the continue in the match expression breaks out of that iteration of the loop entirely, rather than assigning the value (which is never, as @kinire98 noted) to the value of guess.

While interlinking can be handy sometimes, it can also add confusion. If you jump from ch. 2 to ch. 19, you are likely to end up even more confused!

In this case, the point of the example of the guessing game is to give you a taste of Rust—the expectation is not that you will understand 100% of it here. That’s what the rest of the book is for!

Net, we will be leaving this example as is, but I am sorry you found it confusing!

chriskrycho avatar Apr 01 '24 21:04 chriskrycho