Code Example Template
if modifier_pressed {
    execute();
} else {
    pass();
}

Deep-Dive System Documentation

Conditional Expressions in Rust

In Rust, if is not a statement; it is an expression. This means that conditional blocks return values that can be assigned directly to variables.

Core Rules

  1. Type Concordance: If you assign the result of an if/else block, both the if and else branches must evaluate to the exact same return type.
  2. No Implicit Coercions: The condition must evaluate strictly to a bool. Numeric zero or null checks are not allowed as implicit conditions.
let active_profile = if modifier_pressed {
    "Photoshop"
} else {
    "Default"
}; // both branches return &'static str

Performance Metrics

  • Storage: Instantly optimized into native CPU jump signals (e.g., je, jne assembly codes).
  • Branch Prediction: Clean structure allows compiler to apply optimal branch weights automatically.
  • Safety: Eliminates initialization bugs by forcing all branches to evaluate.

Quick Reference Guide

let val = if condition { 1 } else { 2 };