Values & References16 / 18
Up next

Methods that mutate

const std = @import("std");

const Counter = struct {
    n: u32 = 0,

    // taking *Counter makes the method mutate in place
    fn inc(self: *Counter) void {
        self.n += 1;
    }
};

pub fn main() void {
    var c = Counter{};
    c.inc();
    c.inc();
    std.debug.print("{d}\n", .{c.n});
}
What you're solving

*Self lets a method change the receiver