Unions

A union is a custom type that has one or more fields, but only one field is active at any time:

const std = @import("std");
const print = std.debug.print;

const Value = union {
    int: i64,
    float: f64,
};

pub fn main() void {
    // Create a Value with 'int' as the active field:
    var v: Value = .{ .int = 3 };
    v.int += 1;
    print("value is {}\n", .{v.int});

    // To change the active field, assign a new value.
    v = .{ .float = 1.23 };
    print("value is {}\n", .{v.float});
}
$ zig run unions.zig
value is 4
value is 1.23

Next example: Tagged Unions.