Search code examples
rustmatchcontinue

Why does Rust's example guessing game allow a match statement with varying return types?


Looking at the guessing game example from the intro book, specifically the part where you use a match statement to perform error-handling:

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

Why doesn't it complain that the different arms of the match statement have different return types? One returns a u32, the other executes a continue statement and doesn't return anything. I thought either all arms of a match statement must execute code, or all arms must return something of the same type as one another.


Solution

  • continue has type ! (AKA "never"), which can coerce into any other type, since no values of it can exist.