Unwrap Safely with if and orelse
Handle null without surprises.
Unwrap Safely with if and orelse is a free Zig Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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);
}Handle the null Branch
Add an else to react when the optional is null. Now both outcomes are covered with no chance of a missing case.
if (maybe) |v| {
use(v);
} else {
handleEmpty();
}The Capture Is Unwrapped
Inside the if body the captured name is the plain inner type, not an optional. The question mark is already gone for you.
if (age) |a| {
// a is i32, not ?i32
}orelse Supplies a Default
The orelse operator unwraps an optional, but if it is null it gives back the value on its right instead.
const a = age orelse 0;orelse Is an Expression
Because orelse returns a value, you can use it inline anywhere, like passing a defaulted number straight into a function call.
print(level orelse 1);orelse Can Run Code
The right side of orelse may be a block. Use it to compute a fallback or even return early from the current function.
const v = lookup() orelse return;Force Unwrap with .?
Writing .? asserts the optional is not null and hands back the value. If it is null in a safe build, your program panics.
const a = age.?;Use .? Only When Certain
Reach for .? only when null is truly impossible. Otherwise prefer if or orelse so an empty value never crashes you.
Choose by Intent
Use if to branch on presence, orelse for a quick default, and .? only when you can prove the value is there.
Safety Is the Default
Each tool forces you to acknowledge null. That is why Zig optionals rarely turn into the surprise crashes seen in other languages.
Quick Check
You want a default of 0 when an optional integer is null. Which fits best?
Recap
You can unwrap optionals with if captures, supply defaults via orelse, or assert with .?. Each one makes you face null on purpose. ✅
Frequently asked questions
Is the “Unwrap Safely with if and orelse” lesson free?
Yes — the full text of “Unwrap Safely with if and orelse” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.
What will I learn in “Unwrap Safely with if and orelse”?
Handle null without surprises. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Zig Academy?
No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Unwrap Safely with if and orelse” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Zig Academy lesson?
Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Declaring Optional Types
- Unwrap Safely with if and orelse
- Optional Pointers and ?*T
- null Versus undefined