Tuples
Tuples are a way to combine values:
const std = @import("std");
const print = std.debug.print;
pub fn main() void {
const a = .{
123,
"Hello",
};
print("a has {d} fields: {d} {s}\n", .{
a.len,
a[0],
a[1],
});
// The second argument to 'print' is a tuple:
print("a is {}, {s}\n", a);
// Combine tuples with ++
const b = a ++ .{ 4, 5 };
print("b is {}, {s}, {}, {}\n", b);
// We can iterate over the fields with 'inline for':
const numbers = .{ 100, 200, 300 };
inline for (numbers) |n|
print("{}\n", .{n});
}The inline for loop runs at compile time. The compiled
program will just contain three calls to print.
$ zig run tuples.zig
a has 2 fields: 123 Hello
a is 123, Hello
b is 123, Hello, 4, 5
100
200
300
Next example: Enums.