Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping over an integer range in Zig

Tags:

zig

Is a while-loop like this the idiomatic way to loop over an integer range in Zig?

var i: i32 = 5;
while (i<10): (i+=1) {
    std.debug.print("{}\n", .{i});
}

I first tried the python-like

for (5..10) |i| {
    // ....

but that doesn't work.

like image 797
xpqz Avatar asked Mar 31 '26 18:03

xpqz


2 Answers

The way you suggested is valid since 0.11.0 (next release). In the meantime I used to do a version of Ali's answer.

for ([_]u32{0} ** 6) |_, i| {
    std.debug.print("{}\n", .{i});
}
like image 106
jahcal Avatar answered Apr 03 '26 17:04

jahcal


for some needing it now, it's working with the 0.12 unstable version:

    for (5..10) |i| {
        std.debug.print("{d}\n", .{i});
    }
like image 39
nessdev Avatar answered Apr 03 '26 17:04

nessdev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!