🦀 Rust Beginner

What are Rust enums and how do they differ from C enums?

Answer

Rust enums are algebraic data types that can hold data in each variant, making them far more powerful than C enums which are just named integers. Each variant can contain different types and amounts of data: enum Shape { Circle(f64), Rectangle(f64, f64), Point }. The standard library's most important enums are Option<T> (either Some(T) or None) and Result<T, E> (either Ok(T) or Err(E)). Enums are used with match expressions for exhaustive pattern matching — the compiler ensures all variants are handled. Internally, Rust enums are stored as tagged unions, so they are stack-allocated and have zero overhead compared to manual tagged union implementations in C.