Conditional values
if is a value wherever an expression is expected, and a statement where a statement starts. As a value it chooses one of two results by a condition, and it takes the place of the conditional operator other languages write c ? a : b.
if-value = "if" condition value-block "else" ( value-block | if-value )
value-block = "{" { statement } [ expression ] "}"
let label = if n % 2 == 0 { "even" } else { "odd" };
When the condition is true the result is the first branch, otherwise the second. The condition runs once, and only the chosen branch is evaluated; the other is skipped, just as an untaken if block is.
No ?: operator
Rux has no conditional operator. c ? a : b is reported where it is written, with the if to write instead:
error: the conditional operator 'c ? a : b' is not supported
help: write 'if c { a } else { b }'
A ? written directly after an expression is postfix propagation of an optional's absence, value?; that is the only meaning ? has after an expression.
Value blocks
Each branch is a block, so it may run statements before it produces its value. A block in value position yields the expression it ends with when that expression is written without a ;:
let label = if ready {
RecordReady();
"ready"
} else {
"waiting"
};
The same holds for the block of a match arm and of a catch arm. A block that ends otherwise yields (), and a block that cannot complete — it ends by returning, throwing, calling a function that never returns, or leaving a loop — yields nothing and does not decide the type. The yielded value leaves the block before the block's own locals are destroyed, so a local can be moved out as its last expression, as in { let made = Build(); move made }.
A ; after the last expression of a block that must yield a value is reported at the ;, rather than as a distant mismatch between the branches:
let value = if flag {
1;
} else {
2
};
error: this block yields '()' because of the ';'
Only these blocks yield values: a function body still needs return, and a loop body or a nested block keeps every ;.
The else branch
An if used as a value needs an else, since there is no value when the condition is false:
error: an 'if' used as a value needs an 'else' branch
else if chains are allowed, and read as a list of cases tested in order:
let sign = if n < 0 { "negative" } else if n == 0 { "zero" } else { "positive" };
Past two or three cases, a match expression often reads better.
The condition
The condition must be a bool. A number is not true or false by itself:
error: condition for 'if' must have type 'bool', but found 'int32'
Write the comparison: if n % 2 != 0 { "odd" } else { "even" }. The condition may also be a binding is test, whose names are in scope in the first branch: if entry is .Some(value) { value } else { 0 }.
Branch types
The if produces one value, so its branches must agree on a type, by the rules for the arms of a match, whichever branch comes first. Two unrelated types are an error rather than a value of either:
let answer = if ready { "yes" } else { 7 };
error: 'if' branch type mismatch: expected 'char8[..]', found 'int'
Where the context fixes the type — an annotated binding, an assignment, a return, a call argument or an aggregate element — every branch takes it, and an unsuffixed literal in either branch must fit it. An annotation of an optional or a sum is the type of every branch:
let wide: uint128 = if n > 2 { 18446744073709551616 } else { 0 };
let found: int32? = if n > 0 { n } else { none };
A branch that leaves — return, throw, break, continue or a call to Panic — does not take part:
func Checked(n: int32) -> int32 {
return if n >= 0 { n } else { Panic("negative") };
}
Inside larger expressions
An if value runs from if to the closing brace of its last branch. Standing alone as an initializer, an assigned value, a return value or an argument, it needs no parentheses. Inside a larger expression, parenthesize it so the reader sees where it ends:
let total = price - (if member { 5 } else { 0 });
An if at the start of a statement is the statement, not a value, so its branches yield nothing — see if.
See also
- if — choosing between statements rather than values
- match — choosing among many values
- Optional propagation — the postfix
? - Learn: Ternary
Assignment
Assignment with = and move, the compound operators, ++ and --, what may be assigned to, writing through references, and why an assignment produces no value.
Casts
Explicit conversion with as: numeric, bool, character and enum casts, range checks, Truncate and saturation, constant checks, and casts that are refused.