3 ms·
It continues to puzzle me when people think of Rust's syntax as particularly exotic. It's much closer to C/Java/JavaScript than e.g. Python or Bash are. Rust s
by hsivonen 9y ago
It continues to puzzle me when people think of Rust's syntax as particularly exotic.
It's much closer to C/Java/JavaScript than e.g. Python or Bash are. Rust still has curly braces for blocks, uses ampersand and asterisk in ways that aren't too far from C, uses dot in a way that's not too far from C or Java and uses less-than and greater-than to denote generics like C++ and Java.
Personally, what keeps tripping me up when moving between languages is either forgetting to put parentheses around "if" conditions in non-Rust languages after writing Rust or having the Rust compiler complain to me about unnecessary parentheses after writing non-Rust code.
But if new languages couldn't do things like omit unnecessary parentheses around the "if" condition or improve readability by moving the return type to come after the function name, that would seem like too big of a restriction on trying to make some syntactic progress.
Edit: Plus it makes sense to have types after the variable name when they are optional in most cases (and then to have them in the same order in function signatures for consistency).
- twic 9y ago> Edit: Plus it makes sense to have types after the variable name when they are optional in most cases (and then to have them in the same order in function signatures for consistency). Since you brought this up, the one thing that really (but irrationally!) winds me up about Rust's syntax is that variables are: let foo: Bar = ... ; And functions are: fn foo() -> Bar { ... } Why not use a colon for the return type of a function as well?
- ordu 9y ago1. Rust mimics math notation. 2. If one needs variable to hold a function, he declares it like this: let foo: fn(i32) -> i32 = ... The second colon would be confusing.
- jomohke 9y ago"returns" and "is of type" are different concepts, so having different syntax for them makes sense. - In your first example, foo is of type "Bar" - In your second, foo is of type "function that returns Bar". ie, if you stored your foos in separate variables they would be: let my_var: Bar = ... vs let my_function: fn() -> Bar = ... If they were to change it to your suggestion the latter would be either awkward (multiple colons?) or inconsistent (different syntax for types and declarations).