Fundamentals8 / 9
Up next

comptime constants

const std = @import("std");

// comptime runs this at build time; the result is a constant
fn factorial(comptime n: u64) u64 {
    var acc: u64 = 1;
    var i: u64 = 2;
    while (i <= n) : (i += 1) acc *= i;
    return acc;
}

pub fn main() void {
    // FACT_5 is baked into the binary, not computed at runtime
    const FACT_5 = comptime factorial(5);
    std.debug.print("{d}\n", .{FACT_5});
}
What you're solving

values computed at compile time