Structs

A struct is a simple container for named fields.

struct Person {
    name: str
    age: int
}

A struct describes a shape of data - nothing more.

There is:

  • no inheritance
  • no hidden behavior
  • no classes
  • no constructors built in

Structs are intentionally simple.

Creating struct instances

Use the struct literal syntax:

alice: Person = Person { 
    name: "Alice", 
    age: 25 
}

Later, a positional form may be added, but named fields are the canonical form (most explicit, no ambiguity).

Accessing fields

print(alice.name)  // Alice
print(alice.age)   // 25

Mutating fields

Struct fields follow the same mutability rules as variables:

alice.age = alice.age + 1

If the struct instance is declared lock, mutation is forbidden.

Last updated on