Control flow
Rule: no if (cond)
Section titled “Rule: no if (cond)”Write if cond { … }, while cond { … }, match value { … }. Parentheses are for grouping only: (1 + 2) * 3.
Use case: choose a tag
Section titled “Use case: choose a tag”if is an expression when both branches exist:
fn tag(age: int) -> String { return if age >= 18 { "adult" } else { "minor" }}
fn main(caps: Caps) -> int { print tag(20) return 0}Statement form still works: if cond { … } else { … }.
Use case: sum a list
Section titled “Use case: sum a list”fn sum_list(xs: Array<int>) -> int { var total = 0 for x in xs { total = total + x } return total}Use case: count to n (exclusive range)
Section titled “Use case: count to n (exclusive range)”fn sum_to(n: int) -> int { var total = 0 for i in 0..n { total = total + i } return total}0..n means 0 <= i < n.
Use case: C-style index loop
Section titled “Use case: C-style index loop”Index must be var (not let):
fn sum_c(n: int) -> int { var total = 0 for var i = 0; i < n; i++ { total = total + i } return total}Use case: skip and stop early
Section titled “Use case: skip and stop early”// fragment-onlyfn odd_sum_until(limit: int) -> int { var i = 0 var acc = 0 while i < limit { i += 1 if i % 2 == 0 { continue } if i > 10 { break } acc = acc + i } return acc}Boolean logic
Section titled “Boolean logic”Keywords, not C punctuation:
// fragment-onlyif a > 0 and not done { return 1}if a == 0 or b == 0 { return 2}Use case: branch on shape (match)
Section titled “Use case: branch on shape (match)”enum Shape { Circle(int), Rect(int, int)}
fn area(s: Shape) -> int { return match s { Circle(r) => 3 * r * r, Rect(w, h) => w * h }}Match must be exhaustive. Arms may be blocks (last expression is the value):
// fragment-onlylet v = match o { Some(x) => { print x x + 1 }, None => 0}Use case: JS-shaped switch / case
Section titled “Use case: JS-shaped switch / case”// fragment-onlyfn area(s: Shape) -> int { return switch s { case Circle(r): 3 * r * r, case Rect(w, h): w * h, }}Same exhaustiveness as match — see Syntax ergonomics.