Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Rust Dictionary

  1. Home
  2. Rust Dictionary
  3. if / else / if let

if / else / if let

Since: Rust 1.0(2015)

In Rust, if is an expression and can return a value. if let is a concise syntax for pattern matching on Option or Result values.

Syntax

if condition {
    // body
} else if condition2 {
    // body
} else {
    // body
}

// Used as an expression (returns a value)
let variable = if condition { value1 } else { value2 };

// if let (pattern-match branching)
if let pattern = value {
    // runs when pattern matches
} else {
    // runs when pattern does not match
}

if / if let Comparison

SyntaxDescription
if conditionStandard conditional branch that evaluates a bool value.
else if conditionChains multiple conditions together.
let x = if ... { } else { }Uses if as an expression to assign a value to a variable. Each branch must return the same type.
if let Some(x) = optRuns only when the Option is Some. More concise than match for a single pattern.
if let Ok(x) = resultRuns only when the Result is Ok.

Sample Code

sample_if_else_if_let.rs
fn classify(n: i32) -> &'static str {
    if n > 0 {
        "positive"
    } else if n < 0 {
        "negative"
    } else {
        "zero"
    }
}

fn main() {
    // Basic if/else
    let score = 75;
    let grade = if score >= 80 {
        "A"
    } else if score >= 60 {
        "B"
    } else {
        "C"
    };
    println!("Grade: {}", grade); // Prints "B"

    // Handle an Option with if let
    let some_value: Option<i32> = Some(42);
    if let Some(v) = some_value {
        println!("Value is {}", v); // Prints "42"
    } else {
        println!("No value");
    }

    // Handle a Result with if let
    let result: Result<i32, &str> = Ok(100);
    if let Ok(n) = result {
        println!("Success: {}", n);
    }

    println!("{}", classify(5));
    println!("{}", classify(-3));
}

Compile with the following command:

rustc if_else_if_let.rs
./if_else_if_let
Grade: Good
Value is 42
Success: 100
Positive
Negative

Common Mistakes

Common mistake 1: forgetting else in if let

if let only handles the Some/Ok case. The None/Err case is silently ignored unless an else branch is added.

fn get_score(name: &str) -> Option<i32> {
    if name == "user_a" { Some(90) } else { None }
}

fn main() {
    // None is silently ignored — may not be the intended behavior
    if let Some(score) = get_score("user_b") {
        println!("score: {}", score);
    }
}

The same logic can also be written as:

fn get_score(name: &str) -> Option<i32> {
    if name == "user_a" { Some(90) } else { None }
}

fn main() {
    // Handle the None case explicitly
    if let Some(score) = get_score("user_b") {
        println!("score: {}", score);
    } else {
        println!("score not found");
    }
}

Common mistake 2: branches of an if expression return different types

When if is used as an expression, all branches must return the same type. Mismatched types cause a compile error.

fn main() {
    let score = 75;

    // Compile error: mismatched types (i32 vs &str)
    // let grade = if score >= 80 { 100 } else { "good" };
}

The same logic can also be written as:

fn main() {
    let score = 75;

    // All branches return &str
    let grade = if score >= 80 {
        "excellent"
    } else if score >= 60 {
        "good"
    } else {
        "fair"
    };
    println!("grade: {}", grade);
}

Notes

Because if in Rust is an expression rather than a statement, you can use it in place of a ternary operator. Each branch must return the same type; a type mismatch causes a compile error.

if let is useful when you only need to handle a single pattern from an Option or Result. When you need to handle multiple patterns, match is clearer and more readable. See match / Pattern Matching for details.

If you find any errors or copyright issues, please .