3 ms·
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 dep
by asdkhadsj 7y ago
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 :)
- steveklabnik 7y agoIt 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 :)
- asdkhadsj 7y agoApologies for the poorly worded question. Regardless, thanks much as always :)
- steveklabnik 7y agoI don't think it was poorly worded; I'm just chugging some coffee and find some discussions easier than others :)
- dbaupp 7y agoI 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`).