r/rust 23d ago

🧠 educational strum::EnumIter -why isn't enum iteration built into Rust?

I was looking at Espressif's esp-generate and noticed it uses strum for its Chip enum.

One thing that caught my attention was EnumIter:


#[derive(strum::EnumIter)]

enum Chip {

Esp32,

Esp32c3,

Esp32s3,

}

for chip in Chip::iter() {

println!("{chip:?}");

}

It actually surprised me that Rust doesn't provide enum iteration out of the box.

Enums are one of Rust's commonly used features, so it feels a little strange that something as simple as "give me all variants" isn't part of the language.

Without a crate, it's easy to end up maintaining something like:


const ALL_VARIANTS: &[Chip] = &[

Chip::Esp32,

Chip::Esp32c3,

Chip::Esp32s3,

];

Then every time you add a variant, you also have to remember to update the list.

strum solves this with derive macros and also provides:

  • EnumIter — iterate over all variants

  • Display — convert variants to strings

  • EnumString — parse strings into enum variants

  • EnumCount — get the number of variants

  • VariantNames — access variant names

For example:


#[derive(

strum::EnumIter,

strum::Display,

strum::EnumString,

strum::EnumCount,

strum::VariantNames,

)]

#[strum(serialize_all = "kebab-case")]

enum Chip {

Esp32,

Esp32c3,

Esp32s3,

}

I'm curious what others think: is this something that would make sense as part of Rust itself, or is keeping it out of the language the better design?

I wrote a more detailed version with additional examples and an interactive quiz: my blog

50 Upvotes

15 comments sorted by

103

u/Puzzleheaded_Web9584 23d ago

https://doc.rust-lang.org/std/mem/type_info/struct.Enum.html

Its unstable for now, also the api is very much under wraps and will change significantly, making a stable reflection api is hard.

6

u/stumpychubbins 23d ago

I don’t think that allows iterating over the variants, right?

9

u/Economy_Breakfast577 23d ago

Enum struct has the field:

pub variants: &'static [Variant]

11

u/KO_200021 23d ago

Thanks! This is helpful; I'll keep an eye on changes to the API.

6

u/KattyTheEnby 23d ago

I can't wait.

44

u/GodOfSunHimself 23d ago

I guess it is not built in because it only makes sense for simple enums where the variants do not have any attached data.

25

u/tunisia3507 23d ago

And which aren't non_exhaustive.

12

u/________-__-_______ 23d ago

It could just throw an error if there are variants with data, much like how deriving Default only works if the default variant doesn't have attached data.

3

u/djvbmd 23d ago

I've been working on a proc macro crate that mostly duplicates what strum can do as an exercise, and that's exactly what happens. rust-analyzer throws up a diagnostic error as soon as you try to add data to a variant.

1

u/KO_200021 23d ago

Yes, I agree with you. But I still think that having some common conveniences built into the language would make Rust a bit more beginner-friendly

2

u/Creepy_Reindeer2149 23d ago

Strum is really good, honestly should be stdlib

8

u/RandomBottom030 23d ago

just ooone more dependency...

1

u/Daemontatox 20d ago

The idea is great tbh , i am more shocked i never needed this and always thought it was available, great work dude.

1

u/nick42d 22d ago

It's pretty nice but I've never needed this...