Structs
Structs are Zig’s most common kind of composite data type, allowing you to
define types that can store a fixed set of named fields. Zig gives no guarantees
about the in-memory order of fields in a struct or its size. Like arrays,
structs are also neatly constructed with T{}
syntax. Here is an example of
declaring and filling a struct.
Struct fields cannot be implicitly uninitialised:
Fields may be given defaults:
Like enums, structs may also contain functions and declarations.
Structs have the unique property that when given a pointer to a struct, one
level of dereferencing is done automatically when accessing fields. Notice how,
in this example, self.x
and self.y
are accessed in the swap function without
needing to dereference the self pointer.