Back to course

Optional Types

Full Course: Zig Programming From Zero to Hero

Optionals

Zig doesn't have a null value for every type. Instead, you must explicitly mark a type as optional using ?.

zig var maybe_value: ?i32 = null; maybe_value = 5;

To access the value, you can use if or orelse:

zig const actual_value = maybe_value orelse 0;

if (maybe_value) |val| { // val is i32 } else { // value is null }