Assigning a u32 to a Result using a match

This is from Chapter 2 of the book:

let guess: u32 = match guess.trim().parse() {
    Ok(num) => num,
    Err(_) => continue,
};

Earlier, it was told that all the arms of a match statement should return identical types. In the above case that is not so. So how does this work? Thanks.

Somebody correct me if I'm wrong, but I believe this is the correct explanation.

The second match arm is considered to return the "never" type !, which is coercible into all other types - in that way you can consider them to be returning the same type. Concretely, because the second match arm never "returns" (continue advances to the next loop iteration), then it doesn't need to return a value.

2 Likes

Thank you. This has been bothering me for a while.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.