Skip to content

Struct

A struct defines an aggregation of “fields”, containing typed values in a fixed order. All values are always present. It is used much like a struct in C.

struct Point {
int32 x;
int32 y;
}

The binary representation of a struct is simply that of all field values in order. This means it’s more compact and efficient than a message.

When you define a struct, you’re promising to never add or remove fields from it. If you do so, you’ll break compatibility with all existing code that uses the struct. If you need to add or remove fields, you should instead define a new struct with a new name and deprecate the old one.

Mutability

By default all fields of a struct are immutable. You can make a field mutable by adding the mut keyword before its type.

mut struct Point {
int32 x;
int32 y;
}

This will allow you to mutate the field in place, rather than having to copy the entire struct.