When you want to count down to zero while using an unsigned index it is a bit inconvenient. I tried for(3..-1) but it seems negative/custom step sizes aren't supported at all.
Here are the variations I have come up with so far:
const len = 3;
{
std.debug.print("=====================\nfor:\n", .{});
const e = len;
for (0..e) |j| { // no ranges that count down
const i = e - j - 1;
std.debug.print("i: {}\n", .{i});
}
}
{
std.debug.print("=====================\nfor 2:\n", .{});
const e = len + 1;
for (1..e) |j| { // no ranges that count down
const i = len - j;
std.debug.print("i: {}\n", .{i});
}
}
{
std.debug.print("=====================\nwhile:\n", .{});
var j: u16 = len;
while (j > 0) : (j -= 1) {
const i = j - 1;
std.debug.print("i: {}\n", .{i});
}
}
{
std.debug.print("=====================\nfor 3:\n", .{});
var i: u16 = len;
for (0..len) |_| {
i -= 1;
std.debug.print("i: {}\n", .{i});
}
}
Currently I am leaning towards for 3, do you have a better one?