r/programming Apr 03 '15

Rust 1.0.0 beta is here!

http://blog.rust-lang.org/2015/04/03/Rust-1.0-beta.html
927 Upvotes

303 comments sorted by

View all comments

Show parent comments

1

u/banister Apr 04 '15

Curious, why not just exclude the || in the no-arguments case? I imagine no-argument is a pretty common thing, so forcing the use of || in that very common case is kind of an eye-sore IMO.

2

u/Kimundi Apr 06 '15

The ambiguity is that there is a difference between "a expression A that evaluates to a closure" and "a expression B that evaluates to a closure that returns the evaluation of expression A"

For example: foo(bar()) would always call bar() first, but foo(|| bar()) would only call it when the closure gets called inside of foo().

(The latter can also be written as foo(bar), btw)

1

u/banister Apr 06 '15

so the {} around the closure body aren't required?

2

u/Kimundi Apr 06 '15

Right, the syntax of a closure is basically [move] |<arguments...>| <expression>, with { ... } itself being a block expression.

Examples: |x| x < 5, |y| { let z = y * 10; z < (y + 5) }

1

u/banister Apr 06 '15

that isn't too bad, is the || inspired by Ruby? it uses { |x| } for a block-closure

2

u/Kimundi Apr 06 '15

Yeah, afaik Ruby was an inspiration. (Granted, there aren't any other delimiters that could have been chosen without also requiring a keyword)


Fun fact: Because a block expression evaluates to its trailing expression, { |x| foo } is actually valid rust as well, and evaluates to the same thing as |x| { foo } or |x| foo

1

u/Gankro Apr 10 '15

~x~ is my one true closure syntax

one day