Zig by Example: Assignment

In Zig, values can be assigned to constants or variables.

const std = @import("std");
pub fn main() !void {

Here, we assign the value true to the bool constant c. Constants are immutable, so the value of c cannot change.

    const c: bool = true;

Here, we assign the value false to the bool variable v. Variables are mutable, so the value of v can change.

    var v: bool = false;
    v = true;

Note that the compiler can often infer types for you.

    const inferred = true;

To create an uninitialized constant or variable, assign undefined to it. Using undefined values will result in either a crash or undefined behavior, so be careful!

    var u: bool = undefined;
    u = true;

Assignments can also be used to ignore expressions.

    _ = c;
    _ = inferred;
}

Next example: Integers.