Sobeston / zig.guide

Repo for https://zig.guide content. Get up to speed with Zig quickly.
https://zig.guide
MIT License
647 stars 170 forks source link

Payload capture with while loop #235

Open nemelu opened 4 months ago

nemelu commented 4 months ago

This example is a little bit confusing because the while condition is redundant:

test "while optional" {
    var i: ?u32 = 10;
    while (i) |num| : (i.? -= 1) {
        try expect(@TypeOf(num) == u32);
        if (num == 1) {
            i = null;
            break;
        }
    }
    try expect(i == null);
}

I think it would be better like this:

test "while optional" {
    var i: ?u32 = 10;
    while (i) |num| {
        try expect(@TypeOf(num) == u32);
        i.? -= 1;
        if (num == 1) {
            i = null;
        }
    }
    try expect(i == null);
}