Skip to content

Iterator

Chung Leong edited this page Jul 20, 2024 · 1 revision

When a struct contains a next function, it'll be treated as an iterator. Zigar will automatically calls this function when you place the struct in a foreach loop:

const Range = struct {
    current: i32,
    end: i32,

    pub fn next(self: *@This()) ?i32 {
        if (self.current < self.end) {
            const value = self.current;
            self.current += 1;
            return value;
        } else {
            return null;
        }
    }
};

pub fn range(start: i32, end: i32) Range {
    return .{ .current = start, .end = end };
}
import { range } from './iterator-1.zig';

for (const value of range(3, 8)) {
    console.log(value);
}
3
4
5
6
7

next should accept a non-const pointer to the struct as its only argument. It should return an optional value. It must also be public. If you forget to place pub at the front of the function declaration, the struct will behave like a normal struct:

const Range = struct {
    current: i32,
    end: i32,

    fn next(self: *@This()) ?i32 {
        if (self.current < self.end) {
            const value = self.current;
            self.current += 1;
            return value;
        } else {
            return null;
        }
    }
};

pub fn range(start: i32, end: i32) Range {
    return .{ .current = start, .end = end };
}
import { range } from './iterator-2.zig';

for (const value of range(3, 8)) {
    console.log(value);
}
[ 'current', 3 ]
[ 'end', 8 ]
Clone this wiki locally