Unlock Your Python Backend Career: Build 30 Projects in 30 Days. Join now for just $54

LATEST rust ARTICLES

Latest rust Articles curated daily by the community.

Looping Through Enum Variants in Rust Using strum

By Ugochukwu Chizaram Omumusinachi · Updated Thu Jun 05 2025

Rust enums are incredibly powerful for modeling data, but newcomers quickly discover a frustrating limitation: you cannot iterate through enum variants natively. This isn't an oversight but a deliberate design choice that keeps enums lightweight and efficient. However, when you need to loop through all possible enum values for configuration systems, command-line tools, or state machines, this limitation becomes a real problem.

The strum crate solves this elegantly by providing derive macros that extend enums with iteration capabilities and much more. We will show you exactly how to use strum to iterate through enum variants, convert enums to strings, parse strings back to enums, and leverage other powerful features that make working with enums much more pleasant.

Consider this simple enum representing colors:

Looping Through Enum Variants in Rust Using strum

You might naturally try to iterate over all variants like this: