100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Rust

if-else Expressions in Rust

Learn how Rust's if-else works as an expression, requires bool conditions, and can return values directly.

Control FlowBeginner6 min readJul 8, 2026
Analogies

Introduction

In Rust, if is not just a statement — it is an expression that evaluates to a value. This means you can use an if-else block directly on the right-hand side of a let binding, avoiding the need for a separate temporary variable. Unlike languages such as C or JavaScript, Rust does not allow implicit conversion from integers or other types to booleans, so the condition in an if must be an actual bool value.

🏏

Cricket analogy: Just as a match referee's decision directly becomes the recorded outcome rather than a separate note, Rust's if-else evaluates directly into a let binding; and like DRS requiring an actual out/not-out signal, Rust requires a real bool, not a runs count treated as true.

Syntax

rust
if condition {
    // executes when condition is true
} else if other_condition {
    // executes when other_condition is true
} else {
    // executes otherwise
}

// as an expression
let result = if condition {
    value_a
} else {
    value_b
};

Explanation

Rust does not require parentheses around the condition, but the curly braces around each branch are mandatory — this removes the classic 'dangling else' ambiguity found in C-like languages. Because if-else is an expression, every branch used in a value-producing position must return the same type; the compiler rejects mismatched branch types at compile time. When if is used purely for side effects (no assignment), the branches typically evaluate to the unit type ().

🏏

Cricket analogy: Rust doesn't need parentheses hugging the over-count like some scorebooks do, but mandatory braces around each innings prevent the "dangling appeal" ambiguity of which bowler a decision applies to; every batting-order branch must return the same run-type, and a pure signal like a wicket celebration resolves to nothing, the unit type.

Example

rust
fn main() {
    let temperature = 15;

    let description = if temperature > 30 {
        "hot"
    } else if temperature > 15 {
        "warm"
    } else {
        "cool"
    };

    println!("It feels {}", description);

    // condition must be bool, this would NOT compile:
    // if temperature { ... }
}

Output

text
It feels cool

Key Takeaways

  • if in Rust is an expression, so it can produce a value usable in let bindings.
  • The condition must be a bool — Rust has no implicit truthy/falsy conversion.
  • Braces around branches are required; parentheses around the condition are optional.
  • All branches used as an expression must evaluate to the same type.
  • else if chains let you express multiple mutually exclusive conditions cleanly.

Practice what you learned

Was this page helpful?

Topics covered

#Rust#RustProgrammingStudyNotes#Programming#IfElseExpressionsInRust#Else#Expressions#Syntax#Explanation#StudyNotes#SkillVeris