Also, `42` will only return at the end of a block. It's different, but not a magical return that can be placed anywhere. The return statement works exactly like C.
"Rust's syntax is different from C" is perhaps the mildest criticism I can think of Rust. Thank you.
I much prefer the decisions Swift made in this area, with no semicolons and implicit returns only for short functions.
This isn't to knock Rust or the work that you guys do, just a part of the language that can be learned, but does impose a speed bump during that learning process.
Rust also lets you use expression returns by omitting semicolons, which is a common programming style. C also lets you omit braces, which is a common programming style. Being common programming styles, you have to deal with them often. Both are a bit confusing to wrap your head around at first. I find that Rust's confusingness is fine because you can't goof up with it; the compiler will error if you do. C on the other hand will happily let you mess up your braces and still compile.
Again, this is different, not worse.
What? Return works exactly like it does in C, it's just not required as much.
> warning: unnecessary parentheses around `if` condition
I'm confused about how reducing the amount of required syntax is "gratuitous syntax."
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.
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.
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.
let lowercased = names.map(name => name.toLowerCase());Want to make a detailed list of your so-called "gratuitous" syntax and invite discussion, point by point, in an open-minded way?
If there is a simpler syntactic way to capture the semantics and design goals of Rust, I think people would be open to hearing it.
I don't think you'll feel the same way after you invite discussion around it.
The reason this is very good is closures, which have the same return semantics as functions, are often a single expression, and are most useful to fit on one line. `.filter(|x| x.foo_bar() < some_other.foo_bar())`. Removing the return statement helps here.
Its a behavior shared with many (but not all) languages that make heavy use of anonymous functions.
Rust doesn't require parens in an if expression because it requires brackets. You could silence the warning and use parens though.
I spent far too much time trying to figure out how to use string and str. That's where it becomes absurd. I'm fine with fighting with the type/memory systems a bit, but when something that seems simple becomes a confusing mess it's just frustration.
There are lots of languages out there that have implicit return, such as scala and ocaml and f#.
This has been true for me as well. Rust seems to have gratuitous syntax. If C did something and there was another way to do it then that other way was chosen.
With C if you want to return the value 42 then you say return 42; With Rust you say 42. There is a return statement; it just can't return anything. If you say if (a == b) Rust says:
I'm willing to put up with this nonsense because of the safety guarantees. But really, it reminds me of Bananas: Edit: I got this a little but not a lot wrong. return 42 is supported but then it isn't clear at all why 42 is necessary. So my complaint of gratuitous syntax stands as does my respect for Rust's safety guarantees.