Enums
Enums model a value chosen from a finite set of variants. A variant may be a tag on its own or carry one or more values. Enums are useful for state machines, parser results, and explicit success or failure.
Simple enums
enum Color {
Red,
Green,
Blue,
}
fn main(): i32 {
let color = Color.Green;
let code = match color {
Color.Red => 1,
Color.Green => 2,
Color.Blue => 3,
};
$println(code);
return 0;
}A match expression must account for the variants that the compiler can see. Use a wildcard only when the fallback is intentional.
Payload variants
A variant can carry typed data:
enum Message {
Quit,
Move(i32, i32),
Write(string),
}
fn describe(message: Message): i32 {
return match message {
Message.Quit => 0,
Message.Move(x, y) => x + y,
Message.Write(text) => 1,
};
}
fn main(): i32 {
$println(describe(Message.Move(3, 4)));
return 0;
}The payload pattern introduces local bindings for the values stored in the variant. Those bindings follow the same ownership rules as other local values.
Generic enums
Enums can be generic:
enum Outcome<T, E> {
Ok(T),
Err(E),
}
fn divide(left: i32, right: i32): Outcome<i32, i32> {
if right == 0 {
return Outcome.Err(1);
}
return Outcome.Ok(left / right);
}
fn main(): i32 {
match divide(20, 4) {
Outcome.Ok(value) => { $println(value); },
Outcome.Err(code) => { $println(code); return 1; },
};
return 0;
}The payload types are selected independently: Outcome<i32, i32> uses an integer for both branches, while other instantiations can carry an owned value and a domain error enum. This toy divide demonstrates enum construction; see Error Handling for checked integer division.
Matching patterns
Vex supports literal patterns, constructor patterns, wildcard patterns, and or-patterns:
fn classify(value: i32): i32 {
return match value {
0 => 0,
1 | 2 | 3 => 1,
_ => 2,
};
}
fn main(): i32 {
$println(classify(2));
return 0;
}Pattern matching is an expression when all arms produce compatible values. Use a block arm when an arm needs several statements:
match value {
0 => { $println("zero"); return 0; }
_ => { $println("other"); return 1; }
}Option and Result
Option and Result are standard enum-shaped values used by the language and library:
Option<T>represents a value that may be absent, withSome(value)andNonevariants.Result<T, E>represents success or failure, withOk(value)andErr(error)variants.
A complete Option example:
fn value_or_zero(value: Option<i32>): i32 {
return match value {
Some(number) => number,
None => 0,
};
}
fn main(): i32 {
let value: Option<i32> = Some(42);
$println(value_or_zero(value));
return 0;
}This example uses the prelude Option; no import or redeclaration is needed. It prints 42 and exits 0.
The convenience methods on Option and Result are documented in the Option API and Result API. Those APIs are evolving more quickly than the core enum syntax.
Constructors and qualification
Constructors can be written with the enum name when qualification is useful:
Outcome.Ok(42)
Outcome.Err(1)For a known expected enum type, the shorter constructor form may also be accepted:
Ok(42)
Err(1)Prefer the qualified form in public examples when several enum types have similarly named variants.
Guidance
Use an enum when the set of valid states is part of the type. Prefer Option over a sentinel value for absence and Result over an integer error code for recoverable failure. Keep payloads small and meaningful; if a variant contains many unrelated fields, a struct is usually clearer.