2.14 Syntax Structures: Expressions and Statements

Rust is expression-based. Most constructs return a value by default.

fn main() {
    let x = 5; // Statement with an expression initializer

    let y = {
        let x = 3;
        x + 1 // No semicolon -> this value is returned
    };

    println!("x = {}, y = {}", x, y); // y = 4
}
  • A trailing semicolon turns an expression into a statement (discarding the value).
  • Blocks { ... } can be used as expressions.

In C, the distinction between expressions and statements is more rigid, and blocks do not directly return values.