Derive Macro strum::EnumIter

source ยท
#[derive(EnumIter)]
{
    // Attributes available to this derive:
    #[strum]
}
Expand description

Creates a new type that iterates of the variants of an enum.

Iterate over the variants of an Enum. Any additional data on your variants will be set to Default::default(). The macro implements strum::IntoEnumIterator on your enum and creates a new type called YourEnumIter that is the iterator object. You cannot derive EnumIter on any type with a lifetime bound (<'a>) because the iterator would surely create unbounded lifetimes.


// You need to bring the trait into scope to use it!
use strum::IntoEnumIterator;
use strum_macros::EnumIter;

#[derive(EnumIter, Debug, PartialEq)]
enum Color {
    Red,
    Green { range: usize },
    Blue(usize),
    Yellow,
}

// It's simple to iterate over the variants of an enum.
for color in Color::iter() {
    println!("My favorite color is {:?}", color);
}

let mut ci = Color::iter();
assert_eq!(Some(Color::Red), ci.next());
assert_eq!(Some(Color::Green {range: 0}), ci.next());
assert_eq!(Some(Color::Blue(0)), ci.next());
assert_eq!(Some(Color::Yellow), ci.next());
assert_eq!(None, ci.next());