by fee1-dead · 5y ago · 0 comments
I guess you could say `->` is too verbose and you can omit it in other languages. Rust has a complex type system and there can be confusing code when you omit `->`.
1. Closure Return Types.
You can define a closure with an explicit return type:
``` let my_closure = |i: u32| -> u64 { i as u64 }; ```
Now how do you omit the arrow here? How do you know `u64` is the return type and not constructing a struct?
2. Parsing stuff
It becomes impossible to parse none-delimited types. Is `fn() fn()` two different types or a function pointer returning a function pointer?
3. Readability
I mean, tokens can be read out loud and omitting it stops making sense.
`fn foo(bar: i32) -> f32` can be read as a "function named 'foo' that takes an argument named bar with type i32 and returns f32". The word returns directly corresponds to the `->` token.
Rust also has the `!` (read: never) type. Poorly formatted code when `->` is omitted is very confusing: `fn a()!` when compared to `fn a()->!`, or just one character generic types: `fn a<T>()T` compared to `fn a<T>()->T`.
Seems like arrow (->) is the best choice for Rust, given arrow's visual distinctiveness and search-ability.