News for hackers. Stuff that matters.
Re:Rust Language Cheat Sheet by ridiculous_fish on Monday August 05, 2019 @04:19AM (#20612277)
As a Rust noob, what's the rhyme or reason for semicolons?

I see the example:

   struct S {}
but here the semicolon is required:

   type T = S;
or you get weird errors. It's hard to predict when a semicolon is required at least for type declarations, is there an underlying principle?
Re:Rust Language Cheat Sheet by lifthrasiir on Monday August 05, 2019 @04:46AM (#20612419)
Every language item ends either with blocks (when a nested structure is required) or `;` (when it's not). They do not mix; if an item uses a block you don't need `;`.

So `type` needs no more blocks and ends with `;`, while `struct S { ... }` doesn't need `;`. Completing an example, there is another form of `struct` that looks like `struct S(u32);` which do need `;` by the aforementioned logic.

(The same logic applies to statements. You may think of an apparent exception like `{ println!("42"); 54 }`, but here `54` is an expression and you can't put a non-expression statement like `let x = 42` there without a semicolon.)

Re:Rust Language Cheat Sheet by kchamplewski on Monday August 05, 2019 @04:22AM (#20612297)
They appear to be similar to the way most other languages like C and Java use semicolons, where a semicolon isn't required after a code block (generally denoted by squiggly brackets), but is required at the end of most (all?) other statements.
Re:Rust Language Cheat Sheet by Sharlin on Monday August 05, 2019 @10:57AM (#20614684)
The most confusing thing ever is, of course, the fact that C struct/union and C++ class declarations do require a semicolon after the closing curly bracket. This is because type declarations are permitted to occur inline in certain contexts, notably as the type specifier in a variable declaration.
Re:Rust Language Cheat Sheet by dbaupp on Monday August 05, 2019 @05:16AM (#20612557)
As others have said the principal is: no semicolons after things that use {}, semicolons after everything else.
Re:Rust Language Cheat Sheet by Setev on Monday August 05, 2019 @05:20AM (#20612574)
This isn't quite true. Semi-colons are required after all expressions in rust (even when braces are present).

e.g. `let x = match y { ... };`

Re:Rust Language Cheat Sheet by masklinn on Monday August 05, 2019 @05:38AM (#20612657)
Technically here the `;` is for the `let`, which is a statement, not for the match expression within it. The braces are part of the match, not of the let. So the let doesn't end with a block, and thus requires a semicolon.
Re:Rust Language Cheat Sheet by asdkhadsj on Monday August 05, 2019 @08:59AM (#20613802)
Weird, I thought it was just something like "If it's an expression, use a semicolon otherwise you're returning it".

Yet everyone here is making a distinction between blocks and not-blocks. Is "my rule" better?

Furthermore, if you say the semicolon is for the let I think it misleads people. Eg if you do `match foo { .. }`, with no let and no semicolon (which is what that rule would suggest), you're actually returning the value of the match. Same goes for a non-match block, too. Eg, `{ .. }` will return the contents. So if you want to prevent that, you use a semicolon. `{ .. };`.

Am I missing something? Why are so many people describing it with the block vs semicolon thing?

Re:Rust Language Cheat Sheet by steveklabnik on Monday August 05, 2019 @10:03AM (#20614286)
> Weird, I thought it was just something like "If it's an expression, use a semicolon otherwise you're returning it".

This is a common misconception, but it isn't true.

Rust is an expression-oriented language. This means that most things are expressions, and evaluate to some kind of value. However, there are also statements. "Item declarations" (stuff like defining structs, etc) and let statements are the two most common kinds of statements. However, there's also an "expression statement", where you can take any expression, add ; to it, and it becomes a statement rather than an expression.

A block has the following grammar:

  BlockExpression :
     {
        InnerAttribute*
        Statements?
     }

  Statements :
        Statement+
     | Statement+ ExpressionWithoutBlock
     | ExpressionWithoutBlock
A block is made of (leaving some things out for simplicity) zero or more statements, where statements is either one or more statements, one or more statements and an expression, or an expression.

Note that blocks are themselves expressions, and so evaluate to a value. The only way that you get "no semicolon for return" is when the expression is in this tail position, when its value is the value of the whole block.

... does that make sense? Perusing https://doc.rust-lang.org/reference/statements-and-expressio... might help.

Re:Rust Language Cheat Sheet by asdkhadsj on Monday August 05, 2019 @10:49AM (#20614618)
TBH I thought it made sense, but I'm still confused on everyone elses description lol.

I feel like what you said is what I said, but with more accuracy and depth. Eg, if my block was:

  {
    foo();
    bar()
    baz();
  }
My statement holds true, no? It's obviously a syntax problem, since I'm trying to define a statement after I define an expression (which would be trying to execute code after a return, effectively).

So.. I'm not arguing (I trust you to be right lol), but I struggle to understand which part of my statement is inaccurate, beyond the simplifications/etc at least.

To word it differently; I still feel what I said..

> If it's an expression, use a semicolon otherwise you're returning it

seems more accurate as a mental cheatsheet than:

> no semicolons after things that use {}, semicolons after everything else.

Since {}'s can so often result in a value returning, defaulting to not using semicolons just because it's a {} seems odd. I would think focusing on if it's an expression would be easier to understand, no?

Appreciate your time as always Steve :)

Re:Rust Language Cheat Sheet by steveklabnik on Monday August 05, 2019 @10:54AM (#20614661)
It is not 100% clear what specifically you're saying to me, to be honest. All I know is, that in the code above, if you expect bar() to return its value from the block, your mental model of how this stuff works is incorrect.

To be honest, my mental model of all of this is "write code, fix it when the compiler complains"; I rarely think about semicolons, and cargo fmt will remove extraneous ones, so it's sometimes hard for me to go anywhere between "these are the rules of the grammar" and "don't think about it at all". My failing, not yours :)

Re:Rust Language Cheat Sheet by asdkhadsj on Monday August 05, 2019 @10:59AM (#20614696)
Apologies for the poorly worded question. Regardless, thanks much as always :)
Re:Rust Language Cheat Sheet by steveklabnik on Monday August 05, 2019 @11:05AM (#20614749)
I don't think it was poorly worded; I'm just chugging some coffee and find some discussions easier than others :)
Re:Rust Language Cheat Sheet by dbaupp on Tuesday August 06, 2019 @04:07AM (#20623203)
I think it is better to not think of ; (or lack of ;) as an "active" return: it isn't like the `return` keyword.

If the last statement/expression in a block is an expression not followed by ;, the block evaluates to that expression. Otherwise (such as, the last expression has a ;), the block evaluates to ().

The function "return" behaviour is because function bodies are blocks: a function returns the value of its block (or any `return`).

Re:Rust Language Cheat Sheet by posixplz on Monday August 05, 2019 @09:34AM (#20614032)
You’re correct. Other folks seem to be using non-Rust mental models for semicolons.
Re:Rust Language Cheat Sheet by masklinn on Monday August 05, 2019 @10:13AM (#20614359)
No they're not, see steve's explanation above.

Putting a semicolon after an expression makes it into a statement, which are unit-valued, which is why the expression's value gets suppressed. It's a subset of "statements not ending with blocks" (since an expression-statement doesn't take a statement-block even if the expression itself has one).

Re:Rust Language Cheat Sheet by asdkhadsj on Monday August 05, 2019 @10:51AM (#20614639)
But my comment was in reply to:

> no semicolons after things that use {}, semicolons after everything else.

Which seems fundamentally flawed for a cheatsheet. Unless you never intend to return anything with a bracket, I guess.

Re:Rust Language Cheat Sheet by dbaupp on Monday August 05, 2019 @06:48AM (#20612966)
Indeed, hence me saying "things that use {}" (like struct {} and trait {}), not "after {}". :)
Re:Rust Language Cheat Sheet by Myrmornis on Monday August 05, 2019 @11:58AM (#20615149)
On the practical side, assuming you set up your editor to run rustfmt on save, then you don't have to think about it because rustfmt will insert delete semicolons if you get it wrong, and then you end up learning anyway.