10.7 Limitations and Considerations

Although Rust’s enums provide significant advantages, there are a few limitations to keep in mind.

10.7.1 Extending Enums

Once defined, an enum’s set of variants is fixed. You cannot add variants externally. This is often seen as a feature because you know all possible variants at compile time. For some use cases, the lack of extensibility might be a downside. If you need to add variants after the enum is defined, traits or other design patterns may be more appropriate.

10.7.2 Matching on Enums

Working with Rust enums generally involves pattern matching, which can sometimes be verbose. However, the compiler ensures that all variants are handled in a match (or using a wildcard _), so you don’t accidentally ignore anything. While this strictness increases reliability, it can lead to additional code. Nonetheless, Rust’s pattern matching is quite flexible, supporting nested structures, conditional guards, and more. We’ll explore advanced pattern matching techniques in Chapter 21.