Hare offers a few ways to perform loops with `for`. There are traditional truth-based loops, for-each loops, and iterators. Iterators are later on.
use fmt;
fn should_stop(counter: size) bool = {
return (counter == 10z);
};
export fn main() void = {
const items = ["one", "two", "three"];
// Traditional loop with incrementing index
for (let i = 0z; i < len(items); i += 1) {
fmt::printf("Index {} has {}\n", i, items[i])!;
};
// Loop forever until a complex condition is met and break out
let counter = 0z;
for (true) {
if (should_stop(counter)) {
break;
};
counter += 2;
fmt::println(counter)!;
};
// For-each loop
for (let item .. items) {
fmt::println(item)!;
};
};
$ hare run loops.ha
4/4 tasks completed (100%)
Index 0 has one
Index 1 has two
Index 2 has three
2
4
6
8
10
one
two
three
Back to table of contents