In Rust, is there a way to iterate through the values of an enum?

I come from a Java background and I might have something like enum Direction { NORTH, SOUTH, EAST, WEST} and I could do something with each of the values in turn with the enhanced for loop like:

for(Direction dir : Direction.values())  {
    //do something with dir
}

I would like to do a similar thing with Rust enums.


Solution 1:

You can use the strum crate to easily iterate through the values of an enum.

use strum::IntoEnumIterator; // 0.17.1
use strum_macros::EnumIter; // 0.17.1

#[derive(Debug, EnumIter)]
enum Direction {
    NORTH,
    SOUTH,
    EAST,
    WEST,
}

fn main() {
    for direction in Direction::iter() {
        println!("{:?}", direction);
    }
}

Output:

NORTH
SOUTH
EAST
WEST

Solution 2:

If the enum is C-like (as in your example), then you can create a static array of each of the variants and return an iterator of references to them:

use self::Direction::*;
use std::slice::Iter;

#[derive(Debug)]
pub enum Direction {
    North,
    South,
    East,
    West,
}

impl Direction {
    pub fn iterator() -> Iter<'static, Direction> {
        static DIRECTIONS: [Direction; 4] = [North, South, East, West];
        DIRECTIONS.iter()
    }
}

fn main() {
    for dir in Direction::iterator() {
        println!("{:?}", dir);
    }
}

If you make the enum implement Copy, you can use Iterator::copied and return impl Trait to have an iterator of values:

impl Direction {
    pub fn iterator() -> impl Iterator<Item = Direction> {
        [North, South, East, West].iter().copied()
    }
}

See also:

  • What is the correct way to return an Iterator (or any other trait)?
  • Why can I return a reference to a local literal but not a variable?

Solution 3:

No, there is none. I think that is because enums in Rust are much more powerful than in Java - they are in fact full-fledged algebraic data types. For example, how would you expect to iterate over values of this enum:

enum Option<T> {
    None,
    Some(T)
}

?

Its second member, Some, is not a static constant - you use it to create values of Option<T>:

let x = Some(1);
let y = Some("abc");

So there is no sane way you can iterate over values of any enum.

Of course, I think, it would be possible to add special support for static enums (i.e. enums with only static items) into the compiler, so it would generate some function which return values of the enum or a static vector with them, but I believe that additional complexity in the compiler is just not worth it.

If you really want this functionality, you could write a custom syntax extension (see this issue). This extension should receive a list of identifiers and output an enum and a static constant vector with these identifiers as a content. A regular macro would also work to some extent, but as far as I remember you cannot transcript macro arguments with multiplicity twice, so you'll have to write enum elements twice manually, which is not convenient.

Also this issue may be of some interest: #5417

And of course you can always write code which returns a list of enum elements by hand.

Solution 4:

I implemented basic functionality in the crate plain_enum.

It can be used to declare a C-like enum as follows:

#[macro_use]
extern crate plain_enum;

plain_enum_mod!(module_for_enum, EnumName {
    EnumVal1,
    EnumVal2,
    EnumVal3,
});

And will then allow you to do things like the following:

for value in EnumName::values() {
    // do things with value
}

let enummap = EnumName::map_from_fn(|value| {
    convert_enum_value_to_mapped_value(value)
})

Solution 5:

You can use an associated constant:

impl Direction {
    const VALUES: [Self; 4] = [Self::NORTH, Self::SOUTH, Self::EAST, Self::WEST];
}

fn main() {
    for direction in Direction::VALUES.iter().copied() {
        todo!();
    }
}