10.7 Limitations and Considerations

10.7.1 Extending Enums

Enums defined in a library module cannot be extended with new variants from other modules.

  • Closed Set: The set of variants is fixed at definition.
  • Workarounds: Use traits or other patterns if extensibility is required.

10.7.2 Matching on Enums

When pattern matching, Rust requires handling all possible variants unless you use a wildcard _.

  • Exhaustiveness: Ensures that all cases are considered.
  • Order Matters: Patterns are checked from top to bottom, and the first match is selected.
  • Default Cases: Use _ => { } to handle unspecified variants.

10.7.3 Pattern Matching Details

  • Pattern Matching: A powerful feature in Rust, allowing for expressive and concise code.
  • Complex Patterns: You can match on nested data, use guards, and destructure complex types.
  • Further Exploration: We'll discuss pattern matching in much more detail in a later chapter.