How to Iterate Over Enum Variants in Rust

Iterate over Rust enum variants by manually listing them in a collection or using external crates like strum for automatic generation.

Use the #[derive(Debug)] attribute on your enum and then iterate over the variants using a match expression or a helper function if you need to access all variants programmatically.

#[derive(Debug)]
enum ShirtColor {
    Red,
    Blue,
}

fn main() {
    let colors = [ShirtColor::Red, ShirtColor::Blue];
    for color in &colors {
        println!("{:?}", color);
    }
}

If you need to iterate over all variants without manually listing them, you must implement a custom iterator or use a macro like strum since Rust does not provide built-in reflection for enums.