Skip to content

Instantly share code, notes, and snippets.

@MadLittleMods
Created November 4, 2023 06:38
Show Gist options
  • Save MadLittleMods/b1ec3e6af74791841703594c59698125 to your computer and use it in GitHub Desktop.
Save MadLittleMods/b1ec3e6af74791841703594c59698125 to your computer and use it in GitHub Desktop.
Reverse iterate over a array/list in Zig
const std = @import("std");
pub fn main() void {
var list = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// `i = i -% 1` is wrapping subtraction.
// `i -%= 1`
var i: u32 = list.len - 1;
while (i < list.len) : (i -%= 1) {
std.log.debug("i: {d} list[{d}] -> {d}", .{ i, i, list[i] });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment