Why are implicit returns at the end allowed, but early returns require the return
keyword?
fn bar() -> u64 {
if true { 1 }
else { 0 }
}
This is is semantically equivalent but raises an error, rust complains I should've used return 1;
instead.
fn foo() -> u64 {
if true { 1 } // early return is an error
0
}
I can't see a technical reason why this shouldn't work, the compiler obviously is able to detect this and a (implicit) return statement is just syntactic sugar. Are there any reasons not to allow implicit early returns?
Why is the compiler asking me to add a return statement here? and the Q&A explain why the example is currently an error:
in Rust a full statement must have type () rodrigo
return x is an expression, too - it just evaluates to never and so can be used everywhere without type mismatch Cerberus
I am instead looking for an answer why this design choice was made.