Skip to content

Instantly share code, notes, and snippets.

@travisstaloch
Last active April 28, 2024 14:02
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save travisstaloch/71a7a2bc260997abe06016c619b40bf2 to your computer and use it in GitHub Desktop.
Save travisstaloch/71a7a2bc260997abe06016c619b40bf2 to your computer and use it in GitHub Desktop.
zig iterate over struct fields and print
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)));
}
}
@AntonioNoack
Copy link

AntonioNoack commented Mar 22, 2023

For anyone else finding this, with the newest build, 0.11.0, the following is correct:

inline for (std.meta.fields(@TypeOf(x))) |f| {
    std.log.debug(f.name ++ " {any}", .{@as(f.type, @field(x, f.name))});
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment