4 ms·
As a hack, you can use an array of u0 to make simple loops: for ([1]u0{0} ** 10) |_, i| { std.debug.print("{d}", .{i}); }
by hiccuphippo 5y ago
As a hack, you can use an array of u0 to make simple loops:
for ([1]u0{0} ** 10) |_, i| {
std.debug.print("{d}", .{i});
}
- tored 5y agoNot a Zig programmer, but that looks horrible.
- AnIdiotOnTheNet 5y agoYeah, it's a hack. If you're doing it a lot you can abstract it into a function to clean it up: fn count(comptime n: usize) [n]u0 { return [1]u0{0} ** n; } for (count(10)) |_, i| { std.debug.print("{d}", .{i}); } or, if you don't need to capture the index: for (count(9)) |_| { //I can do it 9 times } Personally I've found I rarely need this kind of loop, so I just use the 'while' formulation because it is more obvious.
- dnautics 5y agoHah! i thought it was not a hack so much as a 'joke' that someone had discovered and posted to the discord, to much amusement. Are people actually using this? I will be amused when std.math.range shows up with this implementation.
- suby 5y agoIs there a performance penalty for this?
- dnautics 5y agosee for yourself: https://godbolt.org/z/Mj9T7vP44 https://godbolt.org/z/Mj9T7vP44