Declaring multiple variables on one line

Hello everyone,

I am trying to declare multiple variables on one line as you would in C.

Is there a way to do that in Zig? I’ve tried enclosing them in parentheses to see if they would work but nothing works.

Is it because every variable and constant has to be defined or marked as undefined?

Is packing them in a struct a way to do that or do I just have to declare each variable on its own separate line? If that’s the case then that’s alright.

Many thanks in advance!

I hope I put this question in the right category.

Just putting them on separate lines is the normal expected way to do things.
If you need to pass around many things consider using a struct.

With recent zig versions there is also destructuring so you also could do this:

const a, const b = .{ 23, 56 };
std.debug.print("a = {} and b = {}\n", .{ a, b });

However doing this is probably not the right thing to do:

const a, const b, const c, const d, const e, const f, const g, const h, const i, const j = .{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
std.debug.print("{} {} {} {} {} {} {} {} {} {}\n", .{ a, b, c, d, e, f, g, h, i, j });
6 Likes

I will look up more on destructuring. Thanks Sze!

1 Like