Looking for syntax help

Hi Members, I am new to rust and finding a lot of boiler plate code, with absolutely no reason, wondering how can I convince myself, when I am coming from the world of most beautiful language which is Kotlin.

This is not working

1..=10.for_each(|i| print!("{},", i))

But this is working

let range = 1..=10;
range.for_each(|i| print!("{},", i))

In fact Rust claims to be more readable by what is this ? then why lambda /closure is using | | instead of (), without having to need of arrow operator (arrow is being used for return type) this is giving me headache, when it comes to readability.

is there anywhere any explanation provided anywhere what to expect and what not to expect ? please share me some readable articles

Parentheses:

(1..=10).for_each(|i| print!("{},", i))

Readability is subjective. Many people (including me) like Rust's syntax and find it readable, but some people don't. That's fine, but arguing about it is not going to lead anywhere productive when the basic syntax is set in stone, as it has been for years.

The closure syntax is borrowed from Ruby, except that Rust uses curly braces instead of do and end to delimit blocks.

11 Likes

Parentheses instead of |...| could result in confusing error messages if a semicolon is omitted. Imagine

  let x = (y)
  f(z);

You would probably be surprised to find x is a closure.

In languages that use parentheses here, an extra arrow can avoid this ambiguity, but rust already uses arrows for two different purposes. The -> arrow is used in types, and the => arrow is used in match statements. Using either of these to do double duty would be a poor choice because again, it could lead to ambiguity or confusion.

It takes time to learn a new syntax, but one that is unambiguous is always more readable, and using symbols to always men the same thing also improves readability.

4 Likes

I wouldn’t second this as an argument alone. Haskell is a nice language IMO and it uses -> for function types and for (its equivalent of) match statements expressions (where Rust uses =>) and also for lambdas/closures. Which are, however, introduces by an extra character, \ (resembling a λ), so that arguably it’s a bit less problematic syntactically compared to just Java-style (x, y) -> …. I just googled Kotlin syntax and it seems to require braces around the whole closure [and, in turn, it does not require parantheses around the parameters at all]? (Correct me if I’m wrong.) Compared to needing to wrap the whole thing, I feel like Rust’s approach is fairly clean as well.

In the end it’s a minor syntactical convention. If you’re learning Rust, then closure syntax will probably turn out to be the least of your worries.

2 Likes

I agree that Haskell is a nice language, but I wouldn't award it points for a clear or simple syntax, and this is an example of that. Arrows go every which way and there are many kinds of arrows, some of which mean different things in different contexts. At first it seems cool that you can define infix functions out of punctuation symbols, until you have to read your code years later... but that's a different story than the built in arrows.

1 Like

There are many beautiful languages.

Rust is a beautiful language.

Just different from Kotlin.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.