News for hackers. Stuff that matters.
Re:A taste of Rust by Dewie on Thursday April 25, 2013 @04:19PM (#5609602)
I've on a few occasions had problems with semicolons in Java: accidently putting a semicolong like this:

if (something()); { somethingOther(); }

The semicolon seemed to shortcut the whole if-block. I don't know why that thing is even allowed by the compiler.

Re:A taste of Rust by ufo on Thursday April 25, 2013 @05:27PM (#5609994)
Being able to write if and while loops with am empty body is due to braces being optional if you only have one statement and due to the empty statement being a legal statement (doing so keepsthings simpler).

As for the second {}, its to allow you to create inner scopes where you can declare variables with a more restricted scope without needing to do something silly like "if(true)".

Empty block statements are OK sometimes with while and for loops when the real action that matters is in the condition:

    while(!trySomething()){} //empty body for the loop
And since these language
Re:A taste of Rust by pcwalton on Thursday April 25, 2013 @05:43PM (#5610100)
This error can't happen in Rust. If blocks must be braced.
Re:A taste of Rust by mcguire on Thursday April 25, 2013 @05:57PM (#5610175)
The semicolon following the conditional is an empty statement; it ends the if statement equivalently to "if (something()) { }". It's not often seen, but sometimes can be found in:

     for (i = 0; a[i] != x; i++) ;
to find the first index equaling x.

Personally, I always use braces in conditional and loop statements, so I'll never have the empty statement semicolon. But that's me.

Re:A taste of Rust by Dewie on Friday April 26, 2013 @09:26PM (#5616426)
> Personally, I always use braces in conditional and loop statements, so I'll never have the empty statement semicolon. But that's me.

But if you have

if (something()); { somethingOther(); }

Then you have a new problem: you intended the braced statement to execute iff the if-statement evaluated to true. But since you put the semicolon after if by accident, now the braced part will execute no matter what the if-condition evaluates to.

(my point was about unintentional use of semicolon after if-statement - in which case always using braces doesn't seem to help.)