[rust-dev] syntactic sugar for blocks in expressions

Niko Matsakis niko at alum.mit.edu
Thu Jan 5 11:03:49 PST 2012

As discussed, I have pushed the changes which allow block syntactic 
sugar in expressions.  I think the best way to describe the rules is as 

- In Rust, a statement is either a declaration or an expression followed 
by a semicolon.  The result of evaluating the expression is ignored.

- For certain expressions, however, the semicolon may be omitted.  In 
that case, the result type of the expression must be unit.  These 
expressions are (a) C-like special forms that begin with `if`, `while`, 
`do`, or `alt`; (b) standalone blocks `{...}`; (c) sugared calls with 
block arguments `expr {|| ...)`.

- The value of a block expression `{a; b; c}` is the value of the final 
expression in the block (`c`, in this case).  If a block consists of 
only statements (e.g., `{a; b;}`) then the value of the block is unit.

When phrased this way, I think the rules seem simple enough (but then I 
would, wouldn't I?).  So yes, let's see how it goes: if it seems to trip 
people up or feels too complex, I'm happy to reverse the changes and 
give up.


More information about the Rust-dev mailing list