Rust Reference ยท Operators and Expressions
Block expressions
Reference for Rust block expressions: { } as values, the trailing expression rule, scopes and drops, labeled blocks with break, const blocks and unsafe blocks.
In Rust a block { ... } is an expression. It runs its statements in order and its value is the final expression without a trailing semicolon; if that expression is missing, the value is (). The same idea makes if, match and loop produce values, and it is why a function body needs no return for its last line.
Blocks also create a scope. Variables declared inside are dropped, in reverse order, when the block ends, and shadowing inside a block does not leak out. There are several block flavors with a keyword in front: 'label: { } (a block you can break out of with a value), const { } (evaluated at compile time), unsafe { }, and async { }.
let size = { let base = 4; base * base }; let found = 'search: { if cond { break 'search Some(1); } None };
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
{ statements; tail_expression }
'label: { ... break 'label value; ... }
const { expression }
unsafe { ... }
async { ... }
async move { ... }
| Form | Type of the whole block |
|---|---|
{ ...; expr } |
the type of expr |
{ ...; } or { } |
() |
| labeled block | the common type of the tail expression and every break 'label value |
const { expr } |
the type of expr; must be computable at compile time |
block whose last statement diverges (return, panic!, loop {}) |
!, which coerces to any type |
Edge cases: break 'label inside a labeled block works from nested loops, but an unlabeled break still refers to the nearest loop. A labeled block only accepts break, never continue.
Usage notes
- The semicolon matters.
{ x + 1 }returns a number;{ x + 1; }returns()and usually triggers a type mismatch ("remove this semicolon" is the compiler hint). - Statement versus expression position: a block at the start of a statement is parsed as a statement and its value is ignored (it must be
()). To use it as an operand, put it in alet, in parentheses, or after an operator. - Temporaries: temporaries in the tail expression live until the end of the enclosing statement before the 2024 edition; in edition 2024 they are dropped before the block's locals. This only matters with guards such as
MutexGuardorRefCellborrows. - Drop order: locals are dropped in reverse declaration order when the block ends;
let _ = expr;drops immediately,let _x = expr;keeps the value until the end of the block. - Labeled blocks replace flag variables. They are the structured alternative to
gotofor "found it, skip the rest" logic across nested loops. constblocks guarantee compile-time evaluation and allow constants that mention generic parameters (const { size_of::<T>() }). They also make[const { Vec::new() }; 4]possible for non-Copyarray initializers.unsafeblocks do not turn off borrow checking; they only allow the extra operations. Keep them as small as possible and comment the invariant.- Closures and
?: a block does not catch?. Failure returns from the enclosing function or closure, not from the block.
Related
- if / else, match, loop
- break and continue
- unsafe and const
- Tutorial: if expressions, Variables, Drop and RAII