Structs and enums
Declaring product and sum types, including exported fields, generic structs, and enum payloads.
Structs
Section titled “Structs”export struct Point: x: i32 export y: i32Fields are private across module boundaries by default; mark individual fields export to expose them (pub is an accepted legacy alias). Code inside the defining module can construct, read, write, and destructure private fields freely.
Construction uses named payloads:
let p: Point = Point(x=1, y=2)Generic structs
Section titled “Generic structs”struct Box[T]: value: TSum types declare variants with case:
enum Result[T, E]: case Ok(value: T) case Err(error: E)export enum Name:exports the type.- Variants currently take one named payload each. The shape
case Variant(field: Type, other: Type)with multiple fields is reserved for a later milestone: pack multiple values into a struct payload for now. - Construction uses named payloads:
Ok(value=42),Err(error=true).
When to reach for which
Section titled “When to reach for which”- Struct when the data is always present together.
- Enum when only one of several shapes is present at a time.
T | Nonewhen a value is optional: use it instead of anenum Option.A | Bfor ad-hoc unions that don’t deserve a named type.
For matching destructuring forms, see Pattern matching.