Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type mismatch when returning from inside if statement [duplicate]

The following code fails to compile:

fn testing() -> i32 {
    let x = 5;
    if x == 5 {
        5
    }
    6
}

with this error:

error E0308: mismatched types. (expected (), found integral variable)

If I put an explicit return in front of the 5, or if I put the 6 inside an else block, everything works fine. What exactly is Rust complaining about?

like image 479
Alsafi Avatar asked Dec 23 '22 06:12

Alsafi


1 Answers

In Rust, nearly everything is an expression, including if-else. if generates a value, but there is no value for the expression if the expression fails (i.e. x != 5). When putting the 6 into the else block, the if-else statement returns an integral (which will be an i32 due to the return type). You can suppress the statement's value with a semicolon.

return 5 (note, that there is no semicolon) is also a statement, which results in (). Now the if expression always returns (), which is fine.

Idiomatically you should prefer the else variant, so you can omit the return:

fn testing() -> i32 {
    let x = 5;
    if x == 5 {
        5
    } else {
        6
    }
}
like image 64
Tim Diekmann Avatar answered Dec 26 '22 00:12

Tim Diekmann