How to iterate over all variants of an enum in a loop in Rust?
Hi! I just started learning Rust and ran into a problem. I have an enum describing colors:
```rust
enum Color {
Red,
Green,
Blue,
Yellow,
}
```
I need to print all possible colors to the console, but I can't figure out how to do it. In other languages (like Python or Java), I would simply use a for loop or get a list of all values, but in Rust I can't find a built-in way.
I tried writing something like:
```rust
for color in Color {
println!("{:?}", color);
}
```
But the compiler gives an error: `Color` does not implement the `Iterator` trait.
I've heard that Rust is a language without runtime reflection, and perhaps enums are not designed for iteration. But it seems strange to me that in a statically typed language there is no way
No answers yet.
Log in to leave an answer