title: If Expressions

In Rust, if is an expression — it returns a value. This means it can be used in place of a ternary operator.

fn example(x: i32) -> &'static str {
    if x > 0 { "positive" } else { "negative" }
}

Or inline with an explicit return:

return if x > 0 { "positive" } else { "negative" };

The last expression in each block has no semicolon — that’s what makes it return the value. Both blocks must have the same type.