23 ms·
Most functions in Rust return, so you can count on the last expression to be returned the vast majority of the time.
by iopq 12y ago
Most functions in Rust return, so you can count on the last expression to be returned the vast majority of the time.
- stormbrew 12y agoWhich begs the question, why even have the trick of the missing semicolon in that case? Why rely on one missing 5 pixel character to say that "this function defies your expectations"? If it's not the norm, I'd rather have an explicit "nil" or something at the end.
- iopq 12y agoWell, if you add a semi-colon where it's not supposed to be you'll get a compile error. If you omit a semi-colon, you'll also get a compile error. They're just there to separate statements (Rust is not whitespace-sensitive). The most common pattern in my Rust code is: `statement; statement; expression` where the expression is what I actually want to return from this function it seems more noisy to say `statement; statement; return expression;`
- rapala 12y agoThe trick is that now the keyword return marks an early return. And as early returns are often exceptional, I find the added visibility nice. On the other hand, every function in Rust, except those that contain an infinite loop, returns something so the keyword at the end of the function body is redundant in my opinion. Also, because function signatures are never infered in Rust, mistakenly adding or forgetting the last semicolon will always result in a type error.
- Havvy 12y agoIt'd be `()` and make it so that every side effecting function would be at minimum two lines long, and be a visible wart. It's pretty easy to tell that if the function returns something, the last line evaluates to that expression even if there's no `return` since if it didn't, it wouldn't compile (or there'd be a dead code warning).