Enums
enum — simple named symbol sets. This is the entire feature: rut has
no data-carrying enums. Heterogeneous data goes through interfaces
(see Interfaces and dispatch); absence goes through
?T (see Builtin generic types).
Syntax
enum := 'pub'? 'enum' Ident '{' member (',' member)* ','? '}'
member := Ident
use ink::{ Logger };
enum Color { Red, Green, Blue }
enum Direction { Up, Down, Left, Right }
entry fn main() {
let log = Logger("t");
log.info(f"{Color::Blue} {Direction::Right}");
}
Blue Right
- An enum is a distinct named type; its members are its values — plain symbols, nothing more. There is no integer under a member to read, set, or spell.
- No data payloads and no computed members — ever. Methods are an
implblock away (see Impl blocks).
Using members
Members are named through the enum and compare as equal singletons:
use ink::{ Logger };
enum Light { Red, Yellow, Green }
entry fn main() {
let log = Logger("t");
let l = Light::Yellow;
log.info(f"{l == Light::Yellow}"); // true — members are immortal singleton cells
}
true
- An enum value renders as its member name in format strings:
f"{Color::Red}"is"Red". - Enums are ordinary shared values: binding shares the singleton.
Exhaustive when
when over an enum must be exhaustive: cover every member (then
else is optional) or add an explicit else arm — a partial when is
a compile error. See Control flow and when.
use ink::{ Logger };
enum Light { Green, Yellow, Red }
fn go() { let log = Logger("t"); log.info("go"); }
fn brake() { let log = Logger("t"); log.info("brake"); }
fn stop() { let log = Logger("t"); log.info("stop"); }
entry fn main() {
let l = Light::Yellow;
when (l) {
Light::Green -> { go(); },
Light::Yellow -> { brake(); },
Light::Red -> { stop(); }, // all members: no else needed
}
}
brake
Impl blocks
Enums take impl blocks — the same two forms a struct or class
takes: non-self methods (called on the enum’s name) and self methods
(called on a value). A marked [iterable] member makes the enum a
for..of target.
use ink::{ Logger };
enum Dial { Low, High }
impl Dial {
fn default() -> Self { return Dial::Low; }
fn flipped(self) -> Dial {
return when (self) {
Dial::Low -> Dial::High,
Dial::High -> Dial::Low,
};
}
}
entry fn main() {
let log = Logger("t");
let d = Dial::default().flipped();
log.info(when (d) {
Dial::Low -> "low",
Dial::High -> "high",
});
}
high
- A non-self method is a namespaced function:
Dial::default().Selfspells the enum, so-> Selfis the return type andDial::Lowmints the member. - A
selfmethod dispatches on the value:d.flipped()— the receiver crosses as the singleton cell it already is. - Method visibility is real:
pub fnexports cross-module, plainfnis module-private (see Modules and visibility).
A marked member makes enum values iterable — for (let v of c) rides
the same desugar as a class’s (see
The iteration protocol):
use ink::{ Logger };
enum Light { Green, Yellow, Red }
impl Light {
[iterable] fn iterate(self, emit: fn(Light) -> bool) {
let mut cur = self;
for (let mut i = 0; i < 3; i += 1) {
if (!emit(cur)) { return; }
cur = when (cur) {
Light::Green -> Light::Yellow,
Light::Yellow -> Light::Red,
Light::Red -> Light::Green,
};
}
}
}
entry fn main() {
let log = Logger("t");
for (let l of Light::Red) {
log.info(f"{l}");
}
}
Red
Green
Yellow
One limit stays: [disposal] is for structs and classes only — the
engine disposes a record’s cell, and an enum member is immortal.
Boundaries
- Where another language would use a union of literals
(
"left" | "right"), rut uses an enum; where it would use a union of shapes, rut uses an interface-typed value (see Interfaces and dispatch). - The
|spelling exists only for bound-only union aliases andrequiresbounds (see Type aliases and union bounds) — a compile-time admission gate, never a runtime union value. - There is no enum↔int cast:
asis the numeric cast only. Enum values cross the host boundary as their runtime identity plus the integer value.