Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

In Rust, if is a control flow construct that evaluates a boolean condition to determine execution paths. Crucially, if in Rust is an expression, not a statement. This means it evaluates to a value and can be bound to variables, passed to functions, or returned from blocks.

Syntax and Structural Rules

The syntax requires the if keyword followed by a condition and a block of code.
if condition {
    // execution block
} else if secondary_condition {
    // execution block
} else {
    // execution block
}
Rust enforces strict structural rules for if expressions:
  1. No Parentheses: Parentheses () around the condition are syntactically valid but idiomatic Rust omits them. The compiler will issue a warning if they are included unnecessarily.
  2. Mandatory Braces: Curly braces {} are strictly required around the execution block, even if the block contains only a single expression.
  3. Strict Boolean Typing: The condition must evaluate to a strict bool type. Rust does not perform implicit type coercion (truthiness). Passing an integer, string, or pointer directly as a condition results in a compile-time type error.
let val = 1;

// Compile error: expected `bool`, found integer
// if val { ... } 

// Valid: explicit boolean expression
if val == 1 { 
    // ...
}

Expression Semantics and Type Matching

Because if is an expression, it yields the value of the final expression evaluated within the executed block. When an if expression is used to assign a value, the compiler enforces strict type uniformity across all branches.
let condition = true;

// The `if` expression evaluates to an `i32`
let result: i32 = if condition {
    5 // No semicolon, making this the return expression of the block
} else {
    10
};
If the branches return disparate types, the compiler will throw a mismatched types error.
let condition = true;

// Compile error: `if` and `else` have incompatible types
let result = if condition {
    5    // type i32
} else {
    "10" // type &str
};

The Unit Type () in if Expressions

If an if expression does not include an else branch, it implicitly evaluates to the unit type (). Consequently, if an if block without an else evaluates to a type other than (), it will trigger a compile-time error.
let condition = true;

// Valid: The block evaluates to (), matching the implicit `else`
if condition {
    println!("Condition met"); // Statement ends in semicolon, yields ()
}

// Compile error: `if` block yields `i32`, but implicit `else` yields `()`
// if condition {
//     5
// }

if let Pattern Matching

Rust provides a specialized variant called if let, which combines if with pattern matching. It is syntactic sugar for a match expression that only cares about a single pattern, ignoring all others.
let target_value = Some(10);

// Evaluates the block only if `target_value` matches the `Some(x)` pattern
if let Some(x) = target_value {
    // `x` is bound to the inner value (10) within this lexical scope
} else {
    // Executes if the pattern does not match (e.g., if it was `None`)
}
The if let construct adheres to the same expression semantics and type-matching rules as a standard if expression.
Master Rust with Deep Grasping Methodology!Learn More