Ruta graveolens  ·  notes from a language experiment  ·  cultivated since 2025

Block Expressions

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

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

A block expression evaluates to the value of its final expression, and its type is the type of that expression. If the block has no final expression (it ends with a statement, or is empty), it evaluates to () and has type ().

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.