Sub topic 2
Statements versus expressions
This chapter assumes you can build and run a program with cargo, know Rust's primitive types, and understand let and let mut. It explains the distinction that most of Rust's syntax rests on: a statement does something, and an expression produces a value.
The two categories
A statement performs an action and does not produce a value. An expression evaluates to a value. Rust has two kinds of statement:
- Declaration statements, such as
let x = 5;or a function definition. - Expression statements, which are an expression followed by a semicolon.
Because let is a statement, it has no value you can use. This code does not compile:
fn main() {
let a = (let b = 6);
}The compiler reports that it expected an expression and found a let statement. Languages like C let you write a = b = 6. Rust does not.
Almost everything else is an expression
Literals, arithmetic, function calls, if, match, loops, and blocks are all expressions. A block { ... } evaluates to the value of its last expression.
fn main() {
let y = {
let x = 3;
x + 1
};
println!("{y}");
}The inner block runs let x = 3; as a statement. The last line, x + 1, has no semicolon, so it is the value of the block. Then y is 4.
The semicolon turns a value into nothing
Adding a semicolon to an expression turns it into a statement and discards its value. The block then evaluates to (), the unit type, which has exactly one value, also written ().
fn add(a: i32, b: i32) -> i32 {
a + b
}
fn main() {
let sum = add(2, 3);
println!("{sum}");
}The body of add ends with a + b and no semicolon, so that value is returned. This is the usual way to return a value in Rust.
Why this matters
The following table shows what each form evaluates to.
| Code | Kind | Value |
|---|---|---|
| `5` | Expression | `5` |
| `5;` | Statement | none, the block gets `()` |
| `let n = 5;` | Statement | none |
| `{ 5 }` | Expression | `5` |
| `{ 5; }` | Expression | `()` |
Since blocks, if, and match are expressions, you can assign their results directly, pass them as arguments, or return them. You will use this in the next chapters, where if/else and match produce values.
How evaluation flows
The diagram shows how a block produces its value.
Expression statements
Some expressions are useful only for what they do, such as a function call. Rust lets you use them as statements by adding a semicolon:
fn main() {
println!("hello");
let n = 2 + 2;
n * 2;
}The last line computes 8 and throws it away. It compiles, though the compiler may warn about an unused result. Block-like expressions, such as if and match, can appear as statements without a trailing semicolon, as long as their type is ().
Exercise
This function fails to compile with "expected i32, found ()". What is wrong, and how do you fix it?
fn square(x: i32) -> i32 {
x * x;
}
Show answer
The semicolon after x * x turns the final expression into a statement, so the body evaluates to () instead of i32. Remove the semicolon: x * x.
Exercise
What value does y hold after let y = { let a = 2; a * 10 };, and what changes if you write a * 10; instead?
Show answer
y is 20. With a * 10; the block evaluates to (), so y has type () and holds the unit value.