Expressions & Flow · 10 min · free lesson

Block Expressions

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

Core idea

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

Coming from C? Functions Are Expressions covers this side by side. · Coming from Node.js? JavaScript functions vs fn covers this side by side.

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 code

let score = {
    let base = 7;
    base * 2
};
println!("{score}");

The final multiplication becomes the value of the block.

Practice starter

let value = {
    let base = 3;
    base + 1;
};

Practise this code in Playground ↗

Diagram for Block Expressions: a Rust code example flows to the lesson rule and the key observation.
The final multiplication becomes the value of the block.

Work through it

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. A function body may end with a value, each branch of an if expression may produce a value, and each match arm may do the same. The pattern lets control flow and data flow stay close together. It also makes scope useful: temporary names can exist only while the value is being computed. A block can be clear and focused; it should not hide a long sequence of unrelated side effects just to produce one final number.

Try assigning a block to a variable with an explicit i32 annotation. First return an integer, then add the accidental semicolon and read the expected-versus-found message. Next create two nested blocks and name which variables remain visible after each closing brace. The closing brace ends the scope; the last unsuffixed expression determines the value that escapes it.

In Rust, a block can do work and then produce a value. The final expression has no trailing semicolon when you want its value to leave the block. Add a semicolon and the block instead yields the unit value (). That tiny punctuation mark often explains an expected i32, found () error. An if expression follows the same idea: each branch must produce a compatible type when the whole if is used as a value.

Trace a block with two let statements and one final calculation. Mark which lines are statements and which expression supplies the result. Next use an if expression to choose a label based on a score. Deliberately add a semicolon to one branch's final expression and read the compiler's type comparison. If a block is not supposed to produce a meaningful value, ending its statements with semicolons is normal. The rule is about what the surrounding code expects.

Try it

Calculate a value inside a block and return it without return.

What success looks like: The binding receives the tail expression's numeric value.

Check your understanding

What type does value have?

Reveal the answer
  1. i32
  2. bool
  3. ()

The semicolon discards the numeric value, leaving the block's unit value.

One more experiment

Add a semicolon after base * 2 and write the diagnostic you expect from the println! line. Remove it again, change base to 10, and predict the printed value.

Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Block Expressions (courseId "basics", lessonId "block-expressions")
Link: https://crabrix.com/learn/basics/block-expressions/
Goal: No semicolon means a tail value; a semicolon turns it into ().
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=basics/block-expressions) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.