Back to course

Enums and Unions

Full Course: Zig Programming From Zero to Hero

Enums and Tagged Unions

Enums

Enums define a set of named constants.

zig const Color = enum { Red, Green, Blue, };

Tagged Unions

Unions can store one of several types. A "tagged union" uses an enum to keep track of which type is being stored.

zig const Payload = union(enum) { Int: i32, Float: f32, None: void, };

var p = Payload{ .Int = 10 };