Skip to main content

Enums and pattern matching

A Rust `enum` is far more powerful than an enum in most languages: each variant can carry its own data. That lets you describe exactly the states your program can be in, with no impossible combinations.

`match` then checks those states exhaustively. Miss a variant and the build fails — which is why refactoring Rust tends to feel like following a checklist rather than hunting for crashes.

This pairing is why `Option` and `Result` need no special language support: they are just enums with good pattern-matching ergonomics.

Run it and change it

The editor below is live: edit anything and the real compiler output updates by itself.

Your Rust