What I meant was the fact that you can omit the "return" keyword:
fn foo(x: i32) -> i32 { x * 2 }
Re:Notes on a Smaller Rust
by steveklabnik on Thursday July 18, 2019 @10:47AM
(#20470362)
Right, but only as the last expression in a block, which is different than what "implicit returns" implies.
It's a bit of a semantic distinction isn't it? In practice what it means is that you add semicolons to the end of almost every line of imperative code in a block, and you leave the semicolon off the last line and this becomes your return value implicitly (without the use of a return keyword).
Re:Notes on a Smaller Rust
by steveklabnik on Thursday July 18, 2019 @10:48AM
(#20470383)
It is a semantic distinction, because "everything is an expression" and "implicit returns" have different semantics.
Thinking of them as implicit returns leads people to believe they can write code like
fn foo(x: bool) -> i32 {
if x {
5
}
// more code
and then they're confused when this doesn't work.
Expressions evaluate to a value, and semicolons take an expression, throw away its value, and evaluate to () instead. That's it.
The other behaviors you're talking about fall out of these semantics, but are also different than what you've said; functions evaluate to a value, so the final expression's value determines the value it evaluates to, but you cannot "implicitly return" from the middle of a function by dropping a ;, for example.