Created
November 4, 2023 06:38
-
-
Save MadLittleMods/b1ec3e6af74791841703594c59698125 to your computer and use it in GitHub Desktop.
Reverse iterate over a array/list in Zig
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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