3 ms·
The difference is that C/Java/Algol have different syntaxes for things-as-expressions and things-as-statements, and you can't put blocks in the things-as-expres
by vertex-four 10y ago
The difference is that C/Java/Algol have different syntaxes for things-as-expressions and things-as-statements, and you can't put blocks in the things-as-expressions. In Rust, blocks are also expressions and so have a result (the result of the last expression in the block), so your expressions inside the if can be as complex as you like.
Since functions also have a block, and the return value of the function is the result of the block, this is much more consistent.
- kazinator 10y agoIn GNU C, you can use a brace-enclosed statement block as an expression. Funny story; years before I became a C programmer, and at a time when I didn't yet study ISO C properly, I discovered and used this extension naturally. I wanted to evaluate some statements where only an expression could be used so I thought, gee, come on, can't you just put parens around it to turn it into an expression and get the value of the last expression as a return value? I tried that and it worked. And of course, if it works it's good (standards? what are those?) Then I tried using the code with a different C compiler; oops! Anyway, this GNU C feature doesn't have as much of an impact as you might think.