Understanding Rust's Expression-Based Syntax

Rust's expression-based syntax allows code blocks to evaluate to values, enabling concise assignments and control flow without explicit return statements.

Rust is expression-based, meaning almost every block of code evaluates to a value, allowing you to omit semicolons to return results directly. This contrasts with statement-based languages where blocks return nothing unless explicitly returned.

fn main() {
    let x = if true { 5 } else { 6 }; // Returns 5
    let y = { let z = 10; z + 1 };    // Returns 11
}