Skip to main content

E0062: if without else cannot produce a value

An if without else evaluates to (): when no branch runs, there is no value. So none of its branches may produce one.

Example (error)​

fn get_number(flag: bool) -> i64 {
if (flag) {
42
}
}

Fix​

Add an else that produces a value of the same type:

fn get_number(flag: bool) -> i64 {
if (flag) {
42
} else {
0
}
}

Or, if the branch was meant to leave the function early, return the value:

fn get_number(flag: bool) -> i64 {
if (flag) {
return 42;
}
0
}