I added one semicolon to my Rust code.
It stopped compiling.
This worked:
fn add(a: i32, b: i32) -> i32 {
a + b
}
Then I wrote this:
fn add(a: i32, b: i32) -> i32 {
a + b;
}
Same calculation.
Same numbers.
Just one ;.
And Rust basically said: Nope.
So what changed?
The missing piece: expressions
In Rust, an expression produces a value.
For example:
5 + 10
produces:
15
So when Rust sees:
fn add(a: i32, b: i32) -> i32 {
a + b
}
the last line is an expression.
It produces an i32.
And because it's the final expression in the function, that value becomes the function's result.
No return needed.
So what did ; do?
Adding the semicolon changes how that expression is used:
a + b;
Now the a + b value isn't being used as the block's final value.
But the function promised:
-> i32
So Rust asks:
"You promised me an
i32. Where is it?"
And that's why this fails.
The fix is surprisingly simple:
fn add(a: i32, b: i32) -> i32 {
a + b
}
Remove the semicolon.
And then Rust gets interesting
Here's something I didn't expect:
let result = {
let price = 100;
let discount = 20;
price - discount
};
println!("{}", result);
The entire block produces a value.
So this:
{
let price = 100;
let discount = 20;
price - discount
}
evaluates to:
80
Which means:
block
↓
price - discount
↓
80
↓
result
That's the part that made the earlier function finally click for me.
Rust isn't just about statements that execute one after another.
Expressions can produce values, and blocks can produce expressions.
And suddenly things like if, match, and function returns start making a lot more sense.
The takeaway
When you see a Rust expression without a semicolon, don't automatically think:
"Someone forgot
;."
Sometimes that missing semicolon is the whole point.
🦀 In Rust, one tiny ; can change whether your code produces a value or simply performs an action.
Top comments (0)