Chapter 29: Rust Enums

Rust enums — one of the most powerful and most loved features in Rust.

Many people call enums the secret weapon of Rust. They let you model choices, states, variants, tagged unions, and sum types in a way that is completely safe, expressive, and prevents entire classes of bugs at compile time.

Let me explain Rust enums like your personal teacher sitting next to you with code open: slowly, step by step, with lots of small-to-real-world examples you can copy-paste and run right now, Hyderabad-flavored analogies (traffic signals, biryani orders, phone battery states), comparisons to other languages, and building from very simple → medium → advanced patterns.

1. What is an Enum? (The Core Idea)

An enum (short for enumeration) lets you define a type that can be one of several possible variants.

  • Each variant is a named choice
  • Variants can carry different kinds of data (different types, different amounts)
  • At any moment, an enum value holds exactly one variant (not multiple, not none)
  • Rust forces you to handle all possible variants (exhaustive matching)

Analogy: Think of ordering biryani at Paradise. You can order:

  • Chicken Biryani
  • Mutton Biryani
  • Veg Biryani
  • Special Haleem (with extra garnish)

Each choice is different — some have chicken, some mutton, some veggies, some have extra data (garnish level). An enum lets you say: “This order is exactly one of these things — and you must handle every possibility.”

2. Defining a Simple Enum (No Data)

Rust
  • No data → just names (like C-style enum)
  • :: to access variants (namespaced under the enum)
  • match forces you to cover every case → no forgotten states

3. Enums with Data (The Real Power)

Variants can hold data — any type, any amount.

Rust
  • Each variant can have different payload
  • Destructuring in match binds variables automatically
  • Compiler error if you forget any variant

4. The Famous Option<T> Enum (Built-in, Extremely Common)

Rust has no null — instead it uses Option<T>:

Rust

(It’s in the prelude — you don’t need to write it.)

Rust

→ Prevents billions of null pointer bugs — compiler forces you to handle None

5. The Famous Result<T, E> Enum (Error Handling)

Rust
Rust

→ Most Rust code uses Result for errors — no exceptions

6. Enums with Methods (via impl)

Rust

7. Common Patterns & Tips (2026 Style)

  • Use enums for state machines (GameState::Playing, GameState::Paused…)
  • Use enums for commands (Message enum above)
  • Use Option for maybe present values
  • Use Result for operations that can fail
  • Prefer if let / while let when you only care about one variant
  • match must be exhaustive — _ wildcard catches remaining cases

Practice Challenge for Today

Create cargo new rust_enums and try:

  1. enum FoodOrder { ChickenBiryani(u8), MuttonBiryani(u8), VegBiryani, SpecialHaleem { spice_level: u8, extra_garnish: bool } }
  2. Write match that prints description + price based on variant
  3. Add method is_spicy(&self) -> bool
  4. Create function that returns Option<FoodOrder> based on input string
  5. Use Result for a fake file-reading function

Want next?

  • Pattern matching deep dive (match, if let, while let, @ bindings)?
  • Enums vs structs — when to use which?
  • Enums with generics (like Option<T>, Result<T, E>)?
  • Or traits on enums/structs (next big topic)?

Just tell me — your Rust teacher from Hyderabad is right here! 🦀🚀

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *