Rustlings errors4: why if/else

I'm doing the rustlings rust training - which I quite enjoy btw. - and stumbled upon following thing I can't find an explanation for / I do not understand:

following implementation works:

impl PositiveNonzeroInteger {
    fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
        if value < 0 {
            Err(CreationError::Negative)
        } else if value == 0 {
            Err(CreationError::Zero)
        }  else {
            Ok(PositiveNonzeroInteger(value as u64))
        }
    }
}

but the next one doesn't:

impl PositiveNonzeroInteger {
    fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
        if value < 0 {
            Err(CreationError::Negative)
        } else if value == 0 {
            Err(CreationError::Zero)
        }   

        Ok(PositiveNonzeroInteger(value as u64))

    }
}

why does the second one not pass the compiler - I feel like I'm missing something important here.
The function either enters the if or the else if and returns an Error, or it reaches the Ok and returns it...

Just typing the value with a semicolon doesn't return it if it isn't part of the last expression in the function. When it isn't the last thing, you can instead assign it to a variable, e.g.:

let err = if value < 0 {
    Err(CreationError::Negative)
} else {
    Err(CreationError::Zero)
};

If you want to return before the last expression, you have to use the return keyword.

Ah, but it doesn't. A function returns the result of the last expression in its body, and in your second snippet that last expression is always Ok(PositiveNonzeroInteger(value as u64)).

In Rust, all branches of all control flow constructs (if, match, loop, etc.) must evaluate to something. In your case, you haven't provided an else branch so the if-elsif expression must evaluate to nothing (()). However the if and else if branches both evaluate to a Result, hence the expected this to be () error.

error[E0308]: mismatched types
 --> src/lib.rs:4:9
  |
3 | /     if x > 5 {
4 | |         x
  | |         ^ expected `()`, found `u32`
5 | |     } else if x < 2 {
6 | |         x+2
7 | |     }
  | |_____- expected this to be `()`
  |
help: you might have meant to return this value
  |
4 |         return x;
  |         ^^^^^^  ^

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.