for is the only loop keyword in Atoll. There is no while and no loop. The
tokens between for and the body select one of four forms.
| Form | Shape | Continues while |
|---|---|---|
| Iteration | for pattern in value { ... } |
the iterable yields another value |
| Condition | for condition { ... } |
the condition is true |
| Pattern consume | for pattern := expression { ... } |
the expression matches the pattern |
| Infinite | for { ... } |
a transfer expression leaves the loop |
All four appear in one function here:
fn survey(values []int) int {
mut total := 0
for value in values {
total += value
}
mut countdown := 3
for countdown > 0 {
countdown -= 1
}
for index in 0..10 {
total += index
}
for {
break
}
return total
}After for, an immediate { selects the infinite form; a top-level :=
selects pattern consumption; a top-level in selects iteration; anything else
is a boolean condition. Delimiters inside the head do not affect that choice.
Iterate over values
for pattern in value evaluates the iterable once, then binds each yielded
element to the pattern. A pin auto-derefs here the same way it does for a
field or a method: for v in xs walks a ^[]T. xs.^ is not required.
fn sum(values []int) int {
mut total := 0
for value in values {
total += value
}
return total
}Over a list, the name is a loan of the element where it sits. Reading it
copies nothing and takes no count, and the list still holds every element after
the loop. Giving the element to something that keeps it — kept.push(name), a
return, a field of a struct — takes one count at that point, for a string
or a list. A value that cannot be copied, such as a Task or an open file, is
refused there (ATOLL1089), and for t in tasks.drain() hands each one over
instead — see Taking a list apart.
fn longest(names []string) string {
mut best := ""
for name in names {
if name.len() > best.len() {
best = name.clone()
}
}
return best
}best = name is the one place this loop takes a count, and only for the name
it keeps.
Ranges are iterable, both exclusive and inclusive:
fn triangle(n int) int {
mut total := 0
for index in 0..n {
total += index
}
for index in 0..=n {
total += index
}
return total
}A Map yields key/value pairs; the top-level tuple shorthand needs no
parentheses:
fn weigh() int {
mut scores := Map.new[string, int]()
scores.put("ada", 3)
scores.put("grace", 5)
mut total := 0
for name, score in scores {
total += score + name.len()
}
return total
}Sets and string character iterators work the same way, and _ discards a
yielded value you do not need:
fn f(text string) int {
mut unique := Set.new[int]()
unique.put(3)
mut total := 0
for value in unique {
total += value
}
for c in text {
total += 1
}
return total
}A walk does not change what it walks
A walk borrows the list it walks and reads it again at the top of every turn,
so a change the walk goes on past is refused: a push, a set, an assignment
to it, or handing it to a mut parameter, by its own name or through a pointer
to it. The same holds for a walk over an adapter of it — xs.map(f),
s.split(","), m.keys() — and for a map or a string. It is a compile error,
never a silent copy:
fn main() int {
mut xs := [1, 2, 3]
for v in xs {
if v < 3 { xs.push(v + 10) }
}
return xs.len()
}Gather the changes the walk asks for and make them after it:
fn main() int {
mut xs := [1, 2, 3]
mut more: []int = []
for v in xs {
if v < 3 { more.push(v + 10) }
}
for v in more { xs.push(v) }
return xs.len()
}What is refused is going on. A change on a path that leaves the walk costs nothing, because the walk never reads past it:
fn main() int {
mut xs := [1, 2, 3]
for v in xs {
if v == 2 {
xs.push(9)
break
}
}
return xs.len()
}The element the walk lent stops at the same change: a number has been copied
out, and a string or a struct read past it is refused (ATOLL1097). A for mut
writes its elements back as it goes, so its body may not change the list on any
path.
Or walk by index. A condition loop walks nothing, so its body may change the list, and it reads the length again on every turn — this one reaches the elements it added:
fn main() int {
mut xs := [1, 2, 3]
mut total := 0
mut i := 0
for i < xs.len() {
v := xs.get(i.to_usize()) ?? 0
total += v
if v < 3 { xs.push(v + 10) }
i += 1
}
return total
}Mutable iteration
for mut name in list binds each element mutably. The loop desugars to
list.set(i, name) at the end of each iteration, so the write lands through
the collection’s own set. That is not parameter write-back (ATOLL1063).
Because it is written back, the name is a copy of the element rather than a
loan of it, so for mut over values that cannot be copied is ATOLL1089.
fn double_all() []int {
mut values := [1, 2, 3]
for mut value in values {
value *= 2
}
return values
}for i, mut value in list is the same loop with the index beside it, and
writes each element back the same way:
fn main() int {
mut values := [1, 2, 3]
for i, mut value in values {
value = value * 10 + i
}
return (values.get(0) ?? 0) + (values.get(1) ?? 0) + (values.get(2) ?? 0) - 21
}The list can be any place a mut write reaches — a field of &mut self, a
field of a mut local, or what a ^[]T parameter points at — and the
write-back lands there, exactly as self.items.set(i, v) written by hand
would:
struct Bag { items []int }
impl Bag {
fn bump(&mut self) void {
for mut v in self.items {
v = v + 1
}
}
}
fn scale(xs &mut []int, k int) void {
for mut v in xs {
v = v * k
}
}
fn main() int {
mut b := Bag { items: [1, 2, 3] }
b.bump()
scale(&mut b.items, 2)
return (b.items.get(0) ?? 0) * 100 + (b.items.get(1) ?? 0) * 10 + (b.items.get(2) ?? 0)
}Two requirements are enforced. The iterable must be a mutable []T binding:
fn f() int {
values := [1, 2, 3]
for mut value in values {
value *= 2
}
return values.len()
}…and it must actually have elements to write back to, which a range does not:
fn f() int {
for mut index in 0..3 {
index += 1
}
return 0
}Write-back happens on normal completion of the iteration and on continue,
after that iteration’s scope cleanup. A break, return, or propagated error
leaves along its own transfer path. Do not use for mut when a partially
transformed list would violate an invariant — build a new list instead:
fn doubled(values []int) []int {
mut out: []int = []
for value in values {
out.push(value * 2)
}
return out
}It nests, and the element it binds is yours
The binding a for mut introduces owns its element, so a &mut self call on
it — set, push, append — is ordinary, and so is a second for mut inside
the first:
fn main() int {
mut xs := [[1, 2], [3]]
mut t: int = 0
for mut ys in xs {
for mut x in ys { x = x * 10 t = t + x }
}
for ys in xs { for x in ys { t = t + x } }
return t
}Both write-backs land: the inner loop’s numbers go into ys, and ys goes
back into xs, so the second pass reads 60 again.
Loop on a condition
for condition re-evaluates a bool before every iteration. If the first check
is false the body never runs.
fn countdown(start int) int {
mut remaining := start
mut ticks := 0
for remaining > 0 {
ticks += 1
remaining -= 1
}
return ticks
}The condition is an ordinary expression, so it can call methods and combine several tests:
fn advance_until(values []int, budget int) int {
mut index: u32 = 0
mut spent := 0
for values.is_not_empty() && spent < budget && index < values.len().to_u32() {
spent += values.get(index) ?? 0
index += 1
}
return spent
}Consume matching values
for pattern := expression re-evaluates the expression at every loop head. A
match runs one iteration with the pattern’s bindings; a failure ends the loop.
struct Inbox { remaining int }
impl Inbox {
fn next(&mut self) int? {
if self.remaining <= 0 { return None }
self.remaining -= 1
return Some(self.remaining)
}
}
fn drain_inbox() int {
mut inbox := Inbox { remaining: 3 }
mut total := 0
for Some(message) := inbox.next() {
total += message
}
return total
}This is repeated pattern matching where the non-matching case exits, which
makes it the natural shape for Option-returning sources: queues, cursors,
hand-written iterators, and protocol readers. Bindings are fresh each iteration
and leave scope when that iteration finishes, including on continue.
Loop indefinitely
A subjectless for repeats until the body transfers control out.
enum Message { Data(int), Shutdown }
struct Channel { pending int }
impl Channel {
fn receive(&mut self) Message {
if self.pending <= 0 { return Shutdown }
self.pending -= 1
return Data(self.pending)
}
}
fn pump() int {
mut channel := Channel { pending: 4 }
mut handled := 0
for {
message := channel.receive()
match message {
Shutdown => break
Data(value) => handled += value
}
}
return handled
}Break with a value
break value makes the loop produce a result. Use the bare for { } form for
this: it has no ordinary exhaustion path, so a value-bearing break is the only
way out and the loop’s type is that value’s type.
fn first_large(values []int) int {
mut index: u32 = 0
answer := for {
if index >= values.len().to_u32() {
break -1
}
value := values.get(index) ?? 0
if value > 10 {
break value
}
index += 1
}
return answer
}An iteration, condition, or pattern-consume loop can also finish through its
head, and that path contributes void. Assigning such a loop to T is
therefore rejected:
fn f(values []int) int {
mut index := 0
result: int = for index < values.len() {
index += 1
break 3
}
return result
}When exhaustion is meaningful, return an Option or keep an explicit result
binding rather than inventing a sentinel:
fn first_large(values []int) int? {
for value in values {
if value > 10 {
return Some(value)
}
}
return None
}Continue
continue abandons the rest of the body and resumes at the loop head: an
iteration loop advances the iterator, a condition loop rechecks its condition,
and a pattern-consume loop re-evaluates and re-matches.
fn sum_positive(values []int) int {
mut total := 0
for value in values {
if value < 0 {
continue
}
if value == 0 {
continue
}
total += value
}
return total
}Labels
Prefix a loop with 'name: and pass the label to break or continue when the
target would otherwise be the innermost loop.
fn scan(rows [][]int) int {
mut visited := 0
'outer: for row in rows {
for cell in row {
if cell == 0 {
break 'outer
}
visited += 1
}
}
return visited
}continue 'label restarts the labeled loop, leaving every inner scope first:
fn count_clean_rows(rows [][]int) int {
mut clean := 0
'rows: for row in rows {
for cell in row {
if cell < 0 {
continue 'rows
}
}
clean += 1
}
return clean
}A labeled break can carry a value out of a labeled bare loop:
fn find_over(groups [][]int, threshold int) int {
mut group_index: u32 = 0
found := 'search: for {
if group_index >= groups.len().to_u32() {
break 'search -1
}
group := groups.get(group_index) ?? []
for item in group {
if item > threshold {
break 'search item
}
}
group_index += 1
}
return found
}The label must name an enclosing loop. Both of these are rejected:
fn f() int {
break
return 0
}fn f(values []int) int {
for value in values {
break 'nope
}
return 0
}A break or continue with no label cannot appear in a loop’s condition,
or in the head of a pattern-consume loop. There it would leave the loop
around that one, and a reader cannot tell which loop it leaves. Rust refuses it
the same way.
fn rows_seen(limit int) int {
mut seen := 0
for seen < limit {
for ({ break }) {}
seen += 1
}
return seen
}front-fail=ATOLL1019 marks a refusal the new compiler gives. Today’s compiler
takes the break as leaving the outer loop.
Label the loop it leaves, and the head says which:
fn main() int {
mut seen := 0
'rows: for seen < 5 {
for ({ break 'rows }) {}
seen += 1
}
return seen
}An iteration loop’s value is evaluated once, before the loop starts, so a
break there leaves the loop around it and needs no label.
Per-iteration cleanup
The loop body is a lexical scope. Values and defers created in one iteration are
cleaned up before the next iteration starts — including on continue.
error IoError { Denied }
struct File { id int }
impl File {
fn open(name &string) File ! IoError {
if name.is_empty() { error Denied }
return File { id: name.len() }
}
fn read(&self) string ! IoError { return "line" }
fn close(&self) void { println("closed ${self.id}") }
}
fn total_size(paths []string) int ! IoError {
mut total := 0
for path in paths {
file := File.open(path)?
defer file.close()
body := file.read()?
if body.is_empty() {
continue
}
total += body.len()
}
return total
}Each iteration closes its own file. A defer written outside the loop would
instead wait for the surrounding scope. A labeled break can leave several
nested loop scopes at once; cleanup runs innermost-first before control reaches
the labeled target.
Putting it together
error ImportError { Malformed { line: int } }
struct Record { key string, weight int }
fn parse_record(raw &string, line int) Record ! ImportError {
if raw.is_empty() { error Malformed { line: line } }
return Record { key: raw.clone(), weight: raw.len() }
}
fn import_rows(rows [][]string) Map[string, int] ! ImportError {
mut totals := Map.new[string, int]()
mut line := 0
'rows: for row in rows {
line += 1
mut row_weight := 0
for raw in row {
if raw.is_empty() {
continue
}
if raw == "SKIP-ROW" {
continue 'rows
}
if raw == "STOP" {
break 'rows
}
record := parse_record(raw, line)?
row_weight += record.weight
totals.put(record.key, record.weight)
}
totals.put("row-${line}", row_weight)
}
return totals
}continue skips one cell, continue 'rows abandons the rest of a row,
break 'rows ends the import, and ? leaves the function entirely with an
ImportError. See Transfers for the
cleanup sequence each of those shares.