âImplicit returnâ gives you the wrong mental model. Itâs not an implicit return. Itâs that Rust is an expression-based language.
As an expression-based languages, most things are expressions, and expressions evaluate to a value. Blocks are expressions, and so the final value of a block is that blockâs value. Functions are blocks.
If Rust truly had âimplicit returnâ, code like this would work:
fn foo(param: bool) -> i32 {
if param { 5 };
println!("param is: {:?}", param);
6
}
But this doesnât work. You have to use return
if you want to produce a value other than the final value of the block.
The official style, then, is to leave off the return
in the trailing position because itâs redundant.
Does that make sense?