Last active
March 10, 2025 05:51
-
-
Save travisstaloch/71a7a2bc260997abe06016c619b40bf2 to your computer and use it in GitHub Desktop.
zig iterate over struct fields and print
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"); | |
test "fields" { | |
const U1s = packed struct { | |
a: u1, | |
b: u1, | |
c: u1, | |
}; | |
const x = U1s{ .a = 1, .b = 0, .c = 0 }; | |
inline for (std.meta.fields(@typeOf(x))) |f| { | |
std.debug.warn(f.name ++ " {}\n", @as(f.field_type, @field(x, f.name))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For anyone else finding this, with the newest build, 0.11.0, the following is correct: