Enums
Declaration
Section titled “Declaration”An enum defines a type with a fixed set of variants.
enum Direction { North, East, West, South,}Variants with data
Section titled “Variants with data”Variants can carry data. A tuple variant has positional fields:
enum IpAddress { V4(int, int, int, int), V6(string),}
let home = IpAddress.V4(127, 0, 0, 1)let loopback = IpAddress.V6("::1")A struct variant has named fields:
enum Shape { Circle { radius: float64 }, Rectangle { width: float64, height: float64 },}
let c = Shape.Circle { radius: 5.0 }let r = Shape.Rectangle { width: 10.0, height: 20.0 }An enum can mix variants with no data, tuple variants, and struct variants:
enum Event { Ready, KeyPress(rune), Click { x: int, y: int },}Generic enums
Section titled “Generic enums”Enums accept type parameters:
enum Cache<T> { Hit(T), Miss,}let address = Cache.Hit("93.184.216.34")Cache<string>