C+
Language · View as Markdown
v0.0.27 is a macOS / AppKit release. That is the supported path. Other platforms are not recommended; wait for a later version.

Control flow

if: statement and expression

if cond { #println("yes"); } else if other { #println("no"); } else { #println("?"); }

let r: i32 = if cond { 1 } else { 2 };

The condition must be bool. if 1 { ... } is a type error: there is no truthiness.

while

var x: i32 = 0;
while x < 10 { x = x +% 1; }

for: ranges and the C-style form

// Range. 0..n is exclusive; 0..=n is inclusive.
for i in 0..10 { #println(i); }

// C-style, with init / cond / step.
for (var i: i32 = 0; i < 10; i = i +% 1) {
    #println(i);
}

Arrays are not directly iterable; iterate by index:

let arr: [i32; 4] = [10, 20, 30, 40];
for i in 0..4 {
    #println(arr[i as usize]);
}

Iterator values from a gen fn and from the stdlib iterator adapters also work in for ... in.

loop / break / continue

var n: i32 = 0;
loop {
    if n == 5 { break; }
    if n % 2 == 0 { n = n +% 1; continue; }
    #println(n);
    n = n +% 1;
}

A loop with no reachable break diverges: control never leaves it. The compiler knows this, so a function that ends in such a loop needs no dead trailing return to satisfy its return type.

fn serve() -> i32 {
    loop {
        // accept and handle a request forever
    }
    // no `return` needed; the loop never exits
}

while let / if let / guard let

Match-binds and exits when the pattern fails (see Pattern matching):

while let Option[i32]::Some(v) = next() {
    #println(v);
}

Each form also takes var in place of let, making the bound value mutable: if var / while var inside the body (fresh per iteration for while var), guard var in the enclosing scope. The let spellings stay frozen.