Ministry of silly loops

Another do-while loop:

test "do-while" {
    var i: u8 = 'a';
    var result: std.ArrayListUnmanaged(u8) = .empty;
    defer result.deinit(std.testing.allocator);
    while (do_blk: {
        i += 1;
        try result.append(std.testing.allocator, i);
        break :do_blk i != 'd';
    }) {}
    try std.testing.expectEqualStrings("bcd", result.items);
}
1 Like

with pub inline fn do(work: void, @"while": bool) bool { _ = work; return @"while"; } you can write while(do({ body-here }, condition)) {} even auto formats so that the condition is the last line and zls inlay hints kind-of make sense

–

Perhaps I am being overly cautious, but this implementation relies somewhat on the left-to-right evaluation order of expressions, and I have not yet confirmed this in the documentation.

–

https://ziggit.dev/t/does-zig-guarantee-left-to-right-order-of-evaluation/2684

–