Skip to content

Derive EnumCount

Peter Glotfelty edited this page Aug 18, 2019 · 1 revision

For a given enum generates implementation of strum::EnumCount, which returns number of variants via strum::EnumCount::count method, also for given enum MyEnum generates const MYENUM_COUNT: usize which gives the same value as strum::EnumCount (which is usefull for array sizes, etc.).

extern crate strum;
#[macro_use]
extern crate strum_macros;

use strum::{IntoEnumIterator, EnumCount};

#[derive(Debug, EnumCount, EnumIter)]
enum Week {
    Sunday,
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
}

fn main() {
    assert_eq!(7, Week::count());
    assert_eq!(Week::count(), WEEK_COUNT);
    assert_eq!(Week::iter().count(), WEEK_COUNT);
}
Clone this wiki locally