0PricingLogin
Zig Academy · Lesson

Unwrap Safely with if and orelse

Handle null without surprises.

You Must Unwrap First

An optional is not the value inside it. Before you can use the contents you have to unwrap it and deal with the null case.

Capture with if

An if on an optional can bind the inner value with a pipe capture. The body runs only when the optional is not null.

if (maybe) |value| {
    use(value);
}

All lessons in this course

  1. Declaring Optional Types
  2. Unwrap Safely with if and orelse
  3. Optional Pointers and ?*T
  4. null Versus undefined
← Back to Zig Academy