Zig Structs and Enums
In the Zig programming language, structs (struct) and enums (enum) are two fundamental data types.
Structs and enums are the two main ways to define and use custom data types.
Structs and enums provide a higher level of data organization and type safety, suitable for different programming scenarios.
- Struct: used to combine related variables into a composite data type. A struct can contain fields and methods, suitable for organizing complex data.
- Enum: used to define a set of named constants, which can be value-less or value-bearing enums. Enums are suitable for representing limited discrete values.
Struct
A struct is a composite data type that allows combining multiple data items of different types into a single type.
The syntax for structs in Zig is as follows:
const structName = struct {
field1: FieldType1,
field2: FieldType2,
// Other fields
};
In the following code, MyStruct is a struct type with three fields: field1 is a 32-bit integer, field2 is a double-precision floating-point number, and field3 is a pointer to a byte array.
struct MyStruct {
field1: i32,
field2: f64,
field3: []const u8,
}
Example
const
std
=
@import
(
"std"
)
;
// Define a struct
const
Person
=
struct
{
name
:
[
]
const
u8,
age
:
u32,
}
;
pub fn main
(
)
void
{
// Create a struct instance
const
person
=
Person
{
.
name
=
"Alice"
,
.
age
=
30
,
}
;
// Access struct fields and format correctly
std.
debug
.
(
"Name: {s}\n"
, .
{
person.
name
}
)
;
// Use {s} to format slices
std.
debug
.
(
"Age: {}\n"
, .
{
person.
age
}
)
;
}
Compile and run the above code; the output is:
Name: Alice
Age: 30
To modify fields in a struct, make sure you use var to define the struct instance so that field values can be modified. If you use const to define the struct instance, its field values cannot be modified.
Example
const
std
=
@import
(
"std"
)
;
// Define a struct
const
Person
=
struct
{
name
:
[
]
const
u8,
age
:
u32,
}
;
pub fn main
(
)
void
{
// Create a struct instance
var person
=
Person
{
.
name
=
"Alice"
,
.
age
=
30
,
}
;
// Output the initial value
std.
debug
.
(
"Initial Name: {s}\n"
, .
{
person.
name
}
)
;
std.
debug
.
(
"Initial Age: {}\n"
, .
{
person.
age
}
)
;
// Modify the value of a struct field
person.
name
=
"Bob"
;
person.
age
=
35
;
// Output the modified value
std.
debug
.
(
"Modified Name: {s}\n"
, .
{
person.
name
}
)
;
std.
debug
.
(
"Modified Age: {}\n"
, .
{
person.
age
}
)
;
}
Compile and run the above code; the output is:
Initial Name: Alice
Initial Age: 30
Modified Name: Bob
Modified Age: 35
Method
In Zig, struct methods are defined using the fn keyword, similar to class methods in other programming languages.
Example
const
std
=
@import
(
"std"
)
;
const
Rectangle
=
struct
{
width
:
u32,
height
:
u32,
// Method to calculate area
fn area
(
self
:
Rectangle
)
u32
{
return
self.
width
self.
height
;
}
}
;
pub fn main
(
)
void
{
var rect
=
Rectangle
{
.
width
=
10
,
.
height
=
5
,
}
;
// Call a struct method
std.
debug
.
(
"Area: {}\n"
, .
{
rect.
area
(
)
}
)
;
}
Compile and run the above code; the output is:
Area: 50
Enum
An enum is a data type consisting of a fixed set of constant values.
The syntax for enums in Zig is as follows:
const enumName = enum {
Variant1,
Variant2,
// Other variants
};
In the following code, MyEnum is an enum type with three possible values: Option1, Option2, and Option3.
enum MyEnum {
Option1,
Option2,
Option3,
}
Example
const
std
=
@import
(
"std"
)
;
// Define an enum
const
Color
=
enum
{
Red,
Green,
Blue,
}
;
pub fn main
(
)
void
{
// Use the enum
const
favoriteColor
=
Color.
Green
;
switch
(
favoriteColor
)
{
Color.
Red
=>
std.
debug
.
(
"Red\n"
, .
{
}
)
,
Color.
Green
=>
std.
debug
.
(
"Green\n"
, .
{
}
)
,
Color.
Blue
=>
std.
debug
.
(
"Blue\n"
, .
{
}
)
,
}
}
Compile and run the above code; the output is:
Green
Value-bearing Enums
Zig allows specifying concrete values for each enum variant, which can be used to represent more information or for comparison.
Example
const
std
=
@import
(
"std"
)
;
// Define a value-bearing enum
const
Status
=
enum
(
u32
)
{
Pending
=
1
,
InProgress
=
2
,
Completed
=
3
,
}
;
pub fn main
(
)
void
{
const
taskStatus
=
Status.
InProgress
;
switch
(
taskStatus
)
{
Status.
Pending
=>
std.
debug
.
(
"Pending\n"
, .
{
}
)
,
Status.
InProgress
=>
std.
debug
.
(
"InProgress\n"
, .
{
}
)
,
Status.
Completed
=>
std.
debug
.
(
"Completed\n"
, .
{
}
)
,
}
}
Compile and run the above code; the output is:
InProgress
Using Enums as Fields
Enums can be used as struct fields, making structs more flexible and powerful.
Example
const
std
=
@import
(
"std"
)
;
const
Status
=
enum
{
Active,
Inactive,
Suspended,
}
;
const
User
=
struct
{
name
:
[
]
const
u8,
status
:
Status,
}
;
pub fn main
(
)
void
{
// Create a User instance
const
user
=
User
{
.
name
=
"Alice"
,
.
status
=
Status.
Active
,
}
;
// Output the name field of User
std.
debug
.
(
"User: {s}\n"
, .
{
user.
name
}
)
;
// Use {s} to format slices
// Use a switch statement to output different states based on status
switch
(
user.
status
)
{
Status.
Active
=>
std.
debug
.
(
"Status: Active\n"
, .
{
}
)
,
Status.
Inactive
=>
std.
debug
.
(
"Status: Inactive\n"
, .
{
}
)
,
Status.
Suspended
=>
std.
debug
.
(
"Status: Suspended\n"
, .
{
}
)
,
}
}
Compile and run the above code; the output is:
User: Alice
Status: Active