Blocks and expression values
Quick answer
An expression evaluates to a value. A block surrounded by braces can also be an expression: if its final expression has no trailing semicolon, that expression supplies the block's value. This lets you keep temporary calculations local while returning a result.
The final expression matters
Statements such as let introduce bindings. A final numeric expression without a semicolon produces a numeric value; adding a semicolon in these ordinary examples makes the block yield unit, written (). Unit is a type with one value and is useful when no meaningful result is returned.
fn main() { let total = { let base = 8; let fee = 2; base + fee }; println!("{total}");}Expected stdout (not executed):
10Only total is needed outside the block; base and fee are local.
fn main() { let value = { 4 + 2 }; let unit = { let _number = 4 + 2; }; println!("{value}, {unit:?}");}Expected stdout (not executed):
6, (){:?} uses debug formatting, which can display unit.
Common mistake
If a calculation block should yield a value, do not accidentally add a semicolon to the final expression.
Try it yourself
Use a block to calculate the area of a rectangle from two temporary bindings and print its result.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Blocks and expression values — practice quiz
Choose one answer per question, then review the explanation.
What is the value of `let result = { let a = 5; a + 3 };`?
What does the block `{ 5 + 3; }` evaluate to?
Why use a block expression for an intermediate calculation?