C+
Language · v0.0.13

Enums: plain and tagged

Plain enums

A plain enum is a C-like set of named constants. It lowers to i32 and is Copy:

enum Color { Red, Green, Blue }

let c = Color::Red;

Tagged enums (sum types)

A tagged enum carries data with each variant. This is how C+ models "one of several shapes", and it is the foundation of error handling:

enum Shape {
    Circle(f64),
    Rectangle(f64, f64),
    Square(f64),
}

let s = Shape::Circle(3.14);

Generic enums

Enums can be generic over a type parameter, written with [T] (not <T>):

enum Maybe[T] {
    Some(T),
    None,
}

let m: Maybe[i32] = Maybe[i32]::Some(7);
let n: Maybe[i32] = Maybe[i32]::None;

Always write the type arguments at the source level: Option[i32]::Some(v), Option[i32]::None. Internal mangled names like Option__i32 exist but are never something you type.

You consume an enum by pattern matching on its variants.