Declaring Optional Types
Add ? to allow a missing value.
Declaring Optional Types is a free Zig Academy lesson on CoddyKit — lesson 1 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.
Sometimes There Is Nothing
Real programs hit cases where a value is simply absent: a lookup misses, a field is empty. Zig models this absence safely and on purpose.
The Optional Type
An optional type can hold either a real value of some type, or the special value null meaning nothing is there.
Add a Leading Question Mark
You make any type optional by writing ? in front of it. So ?i32 is an integer that might also be null.
var age: ?i32 = null;A Value Fits Right In
An optional happily holds a normal value too. You assign 42 to an ?i32 exactly as you would assign to a plain i32.
var age: ?i32 = 42;null Is the Empty State
The keyword null is the literal that means there is no value. Only optional types are allowed to hold it.
const missing: ?u8 = null;Non-Optionals Reject null
A plain i32 can never be null. The compiler stops you, so absence stays visible in the type rather than hiding as a bad value.
var n: i32 = null; // error: expected i32Wrap Any Type You Like
Optionals are not just for numbers. You can mark a bool, a struct, a slice, or any type as optional by adding the question mark.
var found: ?bool = null;Inference Still Works
Assign an optional value and Zig infers the optional type for you, so you do not always have to spell it out by hand.
const score: ?u32 = 10;Why Not Use a Sentinel
In C people fake absence with values like -1 or a null pointer. Zig gives you a real optional so absence cannot be confused with data.
Absence Is Part of the Type
Because ? lives in the type itself, anyone reading your code instantly sees that a value might be missing and must be handled.
Defaulting to null
It is common to start an optional at null and fill it in later once you actually have the value to store.
var result: ?[]const u8 = null;Quick Check
How do you declare a u32 that might be missing?
Recap
You met optionals: write ? before a type to allow a real value or null. Absence now lives in the type, plain and visible. ❓
Frequently asked questions
Is the “Declaring Optional Types” lesson free?
Yes — the full text of “Declaring Optional Types” 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 “Declaring Optional Types”?
Add ? to allow a missing value. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Declaring Optional Types” 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