4

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.

Nathan Pierson
  • 5,461
  • 1
  • 12
  • 30
  • Ok is for success and carries the value into the parameters which is then returned to the variable well as Err carries takes the Errors being handled and executes the exceptions provided thus continues. – Ject Caleb Oct 11 '20 at 16:48
  • 3
    The book expains it [later](https://doc.rust-lang.org/book/ch19-04-advanced-types.html#the-never-type-that-never-returns). – mkrieger1 Dec 17 '20 at 09:44

1 Answers1

4

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

Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45