v0.4.0

Loops

Rux has four loops. Each runs a block repeatedly; they differ in when, or whether, they test a condition.

LoopTestsThe body runs
while c { … }before each passzero or more times
do { … } while c;after each passat least once
loop { … }neveruntil a break or return
for x in s { … }for a next element of sonce per element
while-loop = [label ":"] "while" condition block
do-while   = [label ":"] "do" block "while" condition ";"
loop       = [label ":"] "loop" block
for-loop   = [label ":"] "for" pattern "in" expression block

Any loop may carry a label, so that a break or continue inside a nested loop can name it — see break and continue. Conditions follow the rules in Statements: a bool, no parentheses needed, a structure literal wrapped in parentheses.

while

while tests its condition before every pass, including the first, so the body may never run:

var i = 0;
while i < 3 {
    PrintLine("{}", i);
    i++;
}

do while

do … while runs the body first and tests afterwards, so the body always runs at least once. The statement ends with ;:

var tries = 5;
do {
    tries--;
} while tries > 10;

The condition is false from the start, but tries is still decremented once, to 4. A continue in the body goes straight to the test.

loop

loop repeats until something leaves it — a break, a return, a throw, or a panic. It says "until I stop it" more directly than while true:

var n = 0;
loop {
    n += 1;
    if n == 4 {
        break;
    }
}

for

for runs its body once for each element of a sequence, binding the element to the loop variable:

let values = [10, 20, 30];
for v in values {
    PrintLine("{}", v);
}

The subject is evaluated once, before the first pass. What for can iterate:

SubjectElements
a fixed-size array T[N]each element, in order
a slice T[..]each element, in order
a string literal or other char8[..]each char8 code unit — a byte of UTF-8, not a character
a range a..b, a..=beach integer from a, up to b exclusive or inclusive
an open range a..each integer from a, until the loop is left
a type with an iteratoreach value its iterator's Next returns
for k in 0..3 {
    Print("{} ", k);
}
for k in 0..=3 {
    Print("{} ", k);
}

This prints 0 1 2 0 1 2 3. An inclusive range visits its end last and stops there, even when the end is its type's maximum, so for v in start..=255 over uint8 never wraps around. A range whose start is past its end runs zero times. When both ends are constants, the compiler catches it instead: for k in 5..2 is error: range start cannot be greater than its end. See Ranges.

Any other type is iterated through the iterator convention: a container declares a parameterless Iterate returning an iterator, and the iterator declares func Next(&var self) -> Item? in its extend block. Each pass calls Next once; a present result is the element, and only the outer none ends the loop. An iterator the loop names as a place — a variable, a field, an element — is advanced where it is, so a loop that stops early leaves it where the walk stopped; the place must therefore be writable, and for x in counter over a let counter is error: cannot advance immutable iterator 'counter'. Any other iterator, such as a call's result, is a temporary the loop advances. See Iteration.

for left in countdown {
    Print("{} ", left);
}

A type with neither is refused:

error: cannot iterate over 'int'
  help: iterate an array, a slice, a range, or a type declaring 'Next' or 'Iterate'

The loop variable

The loop variable is fresh on every pass and immutable: i = 10; inside for i in 0..3 is error: cannot modify immutable variable 'i', and it cannot be declared var. Bind a mutable copy inside the body when one is needed.

It may also be a pattern that matches every element — names, _, and tuples and structures made of them — which takes each element apart:

for (key, value) in pairs {
    PrintLine("{} {}", key, value);
}
for Point { x, y } in points {
    PrintLine("{} {}", x, y);
}

The element is moved into the pattern, so an owning part is the body's to keep or let go of. A pattern that could fail to match, such as .Some(value) or (0, value), is error: a 'for' pattern must match every element; bind the element and test it in the body with if item is … instead.

Over an array or a slice, each pass gets a copy of its element, destroyed at the end of the pass, so the sequence keeps its elements. Only plain data copies, so a direct loop over move-only elements, such as Strings, is refused; loop over the indices and borrow each element in place:

error: 'for' cannot copy elements of move-only type 'Handle' out of 'Handle[..]'
  note: a direct loop over an array or a slice gives each pass a copy of its element
  help: loop over the indices and borrow each element in place

Moving inside a loop

A loop body runs again from where its last pass ended, so a value one pass moves out is not there for the next. Moving an outer variable inside a loop is an error unless the same pass gives it a new value before the loop repeats, or leaves the loop with break, return or throw after the move:

for i in 0..3 {
    Consume(move buffer);
}
error: value 'buffer' may have been moved on some control-flow paths

The loop variable, and bindings declared inside the body, are new on every pass and may be moved freely. See Copy and move.

See also