Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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 impl block 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(). Self spells the enum, so -> Self is the return type and Dial::Low mints the member.
  • A self method dispatches on the value: d.flipped() — the receiver crosses as the singleton cell it already is.
  • Method visibility is real: pub fn exports cross-module, plain fn is 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 and requires bounds (see Type aliases and union bounds) — a compile-time admission gate, never a runtime union value.
  • There is no enum↔int cast: as is the numeric cast only. Enum values cross the host boundary as their runtime identity plus the integer value.