Fundamentals6 / 9
Up next

if and switch

const std = @import("std");

fn describe(n: i32) []const u8 {
    // switch is exhaustive; ranges use ...
    return switch (n) {
        0 => "zero",
        1...9 => "small",
        else => "big",
    };
}

pub fn main() void {
    // if is an expression that yields a value
    const sign = if (-4 < 0) "neg" else "pos";
    std.debug.print("{s} {s}\n", .{ sign, describe(42) });
}
What you're solving

if as an expression and switch ranges