Back to course

Structs: Organizing Data

Full Course: Zig Programming From Zero to Hero

Structs

Structs are used to group related data. In Zig, structs can also have functions (methods).

zig const Point = struct { x: f32, y: f32,

pub fn init(x: f32, y: f32) Point {
    return Point{ .x = x, .y = y };
}

};

const p = Point.init(0.5, 1.2);

Zig structs have no hidden padding by default if you use packed struct, otherwise, the compiler might optimize the layout.