Zig Error: switch must handle all possibilities
error: switch must handle all possibilities means your switch has no prong for some value the expression could have. Zig’s switch must be exhaustive: every possible value has to lead somewhere. For an enum or an error set, a note names exactly what’s missing. For integers, there’s no note, because the missing values would be most of the number range, and the fix is an else prong. For the other errors in this series, see Zig Errors Explained.
The error message
Section titled “The error message”const std = @import("std");
const Color = enum { red, green, blue };
pub fn main() void { const c: Color = .green; switch (c) { .red => std.debug.print("stop\n", .{}), .green => std.debug.print("go\n", .{}), }}main.zig:7:5: error: switch must handle all possibilities switch (c) { ^~~~~~main.zig:3:34: note: unhandled enumeration value: 'blue'const Color = enum { red, green, blue }; ^~~~main.zig:3:15: note: enum 'main.Color' declared hereconst Color = enum { red, green, blue }; ^~~~~~~~~~~~~~~~~~~~~~~~~The first note is the useful one: unhandled enumeration value: 'blue', with a caret on blue in the enum. If several values are missing, you get one note per value.
What it means
Section titled “What it means”A switch in Zig is an expression that must always produce a result or run some prong. If a value could slip through with no prong, the program wouldn’t know what to do, so Zig refuses to build it. This is one of Zig’s most useful checks: when you add a new value to an enum later, every switch that forgot about it shows up as an error. In C, a missing case just does nothing; in Zig, you find out while compiling. The reference’s Exhaustive Switching section has the rule and a short example that fails with this same message. Zig Switch covers the basics.
How to fix it
Section titled “How to fix it”Fix 1: Add the missing prong
Section titled “Fix 1: Add the missing prong”For enums, list the missing value. This is almost always the best fix:
const std = @import("std");
const Color = enum { red, green, blue };
pub fn main() void { const c: Color = .green; switch (c) { .red => std.debug.print("stop\n", .{}), .green => std.debug.print("go\n", .{}), .blue => std.debug.print("calm\n", .{}), }}Output:
goIf several values do the same thing, put them in one prong with commas: .green, .blue => ....
Fix 2: Add else for numbers
Section titled “Fix 2: Add else for numbers”An integer has far too many values to list. A u8 alone has 256:
const std = @import("std");
pub fn main() void { var n: u8 = 2; _ = &n; const word = switch (n) { 1 => "one", 2 => "two", 3 => "three", }; std.debug.print("{s}\n", .{word});}main.zig:6:18: error: switch must handle all possibilities const word = switch (n) { ^~~~~~No note this time. Add an else prong for “everything else”:
const std = @import("std");
pub fn main() void { var n: u8 = 2; _ = &n; const word = switch (n) { 1 => "one", 2 => "two", 3 => "three", else => "many", }; std.debug.print("{s}\n", .{word});}Output:
twoRanges help too: 4...9 => "a few" covers 4 to 9 (three dots, both ends included). Strings can’t be switched on at all; use if with std.mem.eql, or turn the string into an enum first with std.meta.stringToEnum.
Fix 3: Handle every error in an error set
Section titled “Fix 3: Handle every error in an error set”switch on an error works like an enum, and the note names the error you missed:
const std = @import("std");
pub fn main() void { const r = std.fmt.parseInt(u8, "300", 10); if (r) |v| { std.debug.print("{d}\n", .{v}); } else |err| switch (err) { error.InvalidCharacter => std.debug.print("not a number\n", .{}), }}main.zig:7:18: error: switch must handle all possibilities } else |err| switch (err) { ^~~~~~main.zig:7:18: note: unhandled error value: 'error.Overflow'parseInt can also fail because the number doesn’t fit in a u8. Handle that case too:
const std = @import("std");
pub fn main() void { const r = std.fmt.parseInt(u8, "300", 10); if (r) |v| { std.debug.print("{d}\n", .{v}); } else |err| switch (err) { error.InvalidCharacter => std.debug.print("not a number\n", .{}), error.Overflow => std.debug.print("too big for a u8\n", .{}), }}Output:
too big for a u8This is a big part of why Zig errors are easy to handle well: the compiler knows every error a function can return and makes sure you’ve thought about each one. See Zig Errors.
The opposite: unreachable else prong
Section titled “The opposite: unreachable else prong”If you list every value and add else, Zig complains about that too:
main.zig:11:14: error: unreachable else prong; all cases already handled else => std.debug.print("other\n", .{}), ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~The else can never run, so remove it.
Where you’ll usually see it
Section titled “Where you’ll usually see it”- After adding a value to an enum or a variant to a tagged union. Every
switchon it lights up. - Switching on numbers like a menu choice or a status code, without
else. - Handling errors from a function with
switch (err), especially standard library functions with several errors. - Tagged unions, which work like enums: list every variant. See Zig Tagged Unions.
Related errors
Section titled “Related errors”- expected type ‘X’, found ‘Y’: when prongs produce values of different types.
- variable of type ‘comptime_int’ must be const or comptime: its cousin
value with comptime-only type 'comptime_int' depends on runtime control flowshows up when switch prongs are plain numbers. Give the result a type. - All Zig error pages: the full list.
Practice
Section titled “Practice”1. Traffic light
Section titled “1. Traffic light”Add the missing state to the enum’s switch so the program builds and prints wait. Don’t use else.
const std = @import("std");
const Light = enum { red, yellow, green, off };
fn action(l: Light) []const u8 { return switch (l) { .red => "stop", .yellow => "wait", .green => "go", };}
pub fn main() void { std.debug.print("{s}\n", .{action(.yellow)});}Hint: the note will name the missing value. What should a driver do at a light that’s off?
Show solution
const std = @import("std");
const Light = enum { red, yellow, green, off };
fn action(l: Light) []const u8 { return switch (l) { .red => "stop", .yellow => "wait", .green => "go", .off => "look both ways", };}
pub fn main() void { std.debug.print("{s}\n", .{action(.yellow)});}Output:
waitListing .off keeps the switch exhaustive. If you later add .flashing to Light, Zig will point you straight at this function.
- Every
switchmust be exhaustive: each possible value needs a prong. - For enums and error sets, the note names each missing value. Add a prong for it.
- For integers, add
else. Ranges like4...9help. - Prefer listing enum values over
else, so new values get caught later. - An
elsewhen everything is listed givesunreachable else prong: remove it.
Next up: unable to resolve comptime value, the error for a runtime value where Zig needs one known at compile time.
Quick check
Section titled “Quick check”Quick check
Read more
Section titled “Read more”- switch in the Zig language reference: prongs, ranges and
else. - Exhaustive switching in the Zig language reference: the rule behind this error.
- enum in the Zig language reference: enums and switching on them.
- Zig Switch: our lesson with more examples.