Enums

An enum is a type with a fixed set of variants — one of several distinct shapes. Variants can carry data ("payloads"), and the compiler forces you to handle every variant when you destructure one.

A first enum

enum Suit { case Hearts case Diamonds case Clubs case Spades } let s: Suit = .Hearts;

The .Hearts shorthand works whenever the type is known from context.

Cases & Payloads

A variant can carry data:

enum Shape { case Circle(radius: Float64) case Rectangle(width: Float64, height: Float64) case Point } let s = Shape.Rectangle(width: 3.0, height: 4.0); let c = Shape.Circle(radius: 2.5);

Payloads can be labeled (as above) or positional:

enum Result[T, E] { case Ok(T) case Err(E) }

Exhaustiveness

When you match an enum, you have to handle every variant. The compiler refuses to compile if you forget one:

<!-- sample: fails E305 --> enum Shape { case Circle(radius: Float64) case Rectangle(width: Float64, height: Float64) case Point } let shape = Shape.Circle(radius: 2.0); let area = match shape { .Circle(radius) => Float64.pi * radius * radius, .Rectangle(width, height) => width * height // .Point is missing — compile error (E305: non-exhaustive match) };

You can use _ as a catch-all when you genuinely want to default. Don't use it as a way to ignore unhandled variants — when you add a new case to the enum later, an _ will silently absorb it instead of pointing you at the call sites that need updating.

For the deeper pattern matching story (destructuring, guards, bindings, nested patterns), see Pattern Matching.