My Rust experience is rather limited, I largely let it fall by the wayside once I discovered Zig, but one thing I really like about it that Zig does not have is (surprisingly) a syntactical one: block expressions.
In Zig, we have labelled blocks to accomplish the same task, but where I most often find these useful is when I a have a basic if/else to set a variable (essentially a ternary) that is too long/complex to be comfortable on a single line. This leaves me with the following the options:
1. Do nothing. Let the expression run long on a single line.
const some_variable_name = if (some_long_condition) some_long_result_if_condition_true else some_long_result_if_condition_false;
2. Use a label.
const some_variable_name = if (some_long_condition) result: {
break :result some_long_result_if_condition_true
} else some_long_result_if_condition_false;
ā¦or alternativelyā¦
const some_variable_name = if (some_long_condition) result: {
break :result some_long_result_if_condition_true;
} else {
break :result some_long_result_if_condition_false;
};
3. Extend it to multiline without braces or label
const some_variable_name = if (some_long_condition)
some_long_result_if_condition_true
else
some_long_result_if_condition_false;
While #3 is the closest to my preferred way, and allows for placing breakpoints normally, I donāt personally like breaking an if statement across multiple lines without braces. I know that formatters and Zigās strictness largely protect against errors like Appleās infamous āgoto failā vulnerability, but my preferred way is to use a block expression (i.e. omit the final semicolon of the block, but still be able to use braces.
const some_variable_name = if (some_long_condition) {
some_long_result_if_condition_true
} else {
some_long_result_if_condition_false
};
I was curious about othersā thoughts on this syntax, and if they feel it would be a welcome addition to the language?