Block Expressions

A block expression is a sequence of statements followed by an optional expression, enclosed in braces.

block_expr = "{" { statement } [ expression ] "}" ;

The type of a block expression is the type of its final expression. If the block ends with a statement, the type is ().

Variables declared in a block are local to that block and shadow any outer variables with the same name.

fn main() -> i32 {
    let x = 1;
    let y = {
        let x = 10;  // shadows outer x
        x + 5
    };
    x + y  // 1 + 15 = 16
}

When a block exits, all local variables declared in that block are destroyed.