Join members of enum?

Given that I have this enum:

enum SpecialSeries {
    DA,
    KT,
    LD,
}

Now I want to build this string

"DA|KT|LD"

in order to use as pattern for RegEx. How should I code with strum?

Thank you.

If you're using strum, then you can add #[derive(Debug, Display, EnumIter)] to the enum. Then you can use SpecialSeries::iter() to get an iterator over the enum, then use either a fold() with the Display implementation or, more simply, join() from itertools.

1 Like

You can also use a custom macro for this:

enum_with_RegEx! {
    #[derive(Debug)]
    pub
    enum MyEnum {
        Foo,
        Bar,
        Baz,
    }
}

fn main ()
{
    assert_eq!(
        MyEnum::REGEX,
        "Foo|Bar|Baz",
    );
}
1 Like

Thanks, looking too advance to me. Will take time to learn more.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.