Zig by Example: For

For loops can be used to iterate over sequences.

const std = @import("std");
const print = std.debug.print;
pub fn main() !void {
    var array = [_]u32{ 1, 2, 3 };

Here, we iterate over array by value, storing a copy of each element in elem. Note that since elem is just a copy, we cannot use it to modify array’s contents.

    for (array) |elem| {
        print("by val: {}\n", .{elem});
    }

To iterate by reference, we can loop over a slice of array and prefix elem with a *. Here, elem is a pointer to an element in array, which we can use to modify array’s contents.

    for (&array) |*elem| {
        elem.* += 100;
        print("by ref: {}\n", .{elem.*});
    }

Here, we iterate over multiple sequences. Note that both sequences must have the same length.

    for (array, &array) |val, *ref| {
        _ = val;
        _ = ref;
    }

You may also specify a range with the start..end syntax. Note that end may be omitted if another sequence is being iterated over as well; the compiler will infer the range’s size.

    for (0.., array) |i, elem| {
        print("{}: {}\n", .{ i, elem });
    }

To ignore the elements of a sequence, use _.

    for (array) |_| {}
}
$ zig run for.zig
by val: 1
by val: 2
by val: 3
by ref: 101
by ref: 102
by ref: 103
0: 101
1: 102
2: 103