Expressions & Flow · 10 min · free with an account

Block Expressions

Lesson preview · about 30%

A block can perform statements and then produce one final value.

Core idea

No semicolon means a tail value; a semicolon turns it into ().

Start with the idea

Most Rust syntax is expression-oriented. The last expression in a block becomes its value when it has no semicolon. Adding a semicolon changes that expression into a statement and the block then produces (), the unit value. This one rule explains function returns, if branches, match arms, and scoped calculations.

Read the braces as a small computation with its own scope. In the example, base exists inside the block, then base * 2 is the final expression. The whole block evaluates to 14, which is assigned to score. There is no separate return statement because the final expression supplies the value. If you put a semicolon after base * 2, that line becomes a statement and the block's value becomes (), the unit type. A variable expecting an integer can no longer receive it.

This rule appears repeatedly in Rust.

Keep learning, for free

The rest of this lesson is waiting for you.

Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.

No payment details. Your account opens the complete Academy.

← All lessons in Rust Basics