3 ms·
The thing that really surprised me about Rust is the significance of a semicolon at the end of a function body. E.g.: fn foo { 1; } Returns unit while
by fiddlerwoaroof 5y ago
The thing that really surprised me about Rust is the significance of a semicolon at the end of a function body. E.g.:
fn foo { 1; }
Returns unit while
fn foo { 1 }
Returns 1. This reminds me of nothing so much as the infamous JavaScript ASI footgun where this returns undefined:
function foo() {
return
2;
}
- c-cube 5y agoIt's not just the function's body, it's any block. Blocks in rust are expressions and return their last statement's value. It's nothing like Javascript's ASI where it's just inserting ; implicitly and only `return` can return a value.
- burntsushi 5y agoIt's also nothing like Javascript because Rust is statically typed. If you accidentally insert a semi-colon (or not), you're almost guaranteed to get a compile error.
- adamnemecek 5y agoThe second function is a syntax error since you are not declaring the return value in the function signature.