3 ms·
> With Rust you say 42. This is, strictly speaking, not true. Rust is an "everything[1] is an expression language", and so if you have some sort of block, it w
by steveklabnik 10y ago
> With Rust you say 42.
This is, strictly speaking, not true. Rust is an "everything[1] is an expression language", and so if you have some sort of block, it will evaluate to its last expression.
But if you want to do an early return, you must write it, or if you don't like the default Rust style, you can write it.
This is consistent with other languages, just not C.
1: not literally everything, but most things.
- CalChris 10y ago> This is consistent with other languages, just not C. TIOBE Index January 2017. The top 10 languages are Java, C, C++, Python, Visual Basic, Javascript, Perl, Assembly, PHP Except for Assembly, where the comparison makes no sense, all of these languages have return expression. Yes, there are other languages that use last expression but they are most definitely not common languages and the most common languages use return expression. It just seems unnecessary. It isn't clear what they got out of it.
- GFK_of_xmaspast 10y agoPerl has implicit return.
- Manishearth 10y agoRuby has a similar pattern. It's a pretty popular language. Correlation is not causation. Having return expressions does not make or break a language. Python and JS do have return expressions in the context of lambdas. I'm not very fond of this pattern in untyped languages (Ruby, Perl) because it can lead to accidental returns, but I'm pretty happy with having it in a typed language.
- serge2k 10y agoYou can do some very nice things with that sort of syntax. Being able to assign values based on an if statement is one example that just makes life a little better. Just a tiny bit. :)
- lobster_johnson 10y agoJavaScript supports implicit return in anonymous functions: let lowercased = names.map(name => name.toLowerCase());
- flukus 10y agoThe thing all those languages have in common is mutability by default. Being able to return values from if/else eliminates a lot of variables that would need to be mutable otherwise.