Enums

Enums

An enum is a closed set of named variants.

[public] enum SortAlgorithm {
    Insertion,
    Merge,
    Quick,
    Heap,
}

Variants are reached through the enum name:

this.sort_algorithm = SortAlgorithm::Merge

Matching

switch over an enum is the usual way to act on a variant:

switch algorithm {
    SortAlgorithm::Insertion => {
        this.sort_insertion(compare)
    }
    SortAlgorithm::Merge => {
        this.sort_merge(compare)
    }
}

Enums across modules

An enum declared in one module can be imported and used as a type in another, including in generic positions. A field, a parameter, or a type argument can all be an imported enum.