Skip to content

Structs and enums

Declaring product and sum types, including exported fields, generic structs, and enum payloads.

export struct Point:
x: i32
export y: i32

Fields 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)
struct Box[T]:
value: T

Sum 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).
  • Struct when the data is always present together.
  • Enum when only one of several shapes is present at a time.
  • T | None when a value is optional: use it instead of an enum Option.
  • A | B for ad-hoc unions that don’t deserve a named type.

For matching destructuring forms, see Pattern matching.