src/user-defined-types/solution.md
# // Copyright 2023 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
{{#include exercise.rs:solution}}
enum variants can carry data. CarArrived(Floor)
carries an integer, and ButtonPressed(Button) carries a nested Button
enum. This allows Event to represent a rich set of states in a type-safe
way.type Floor = i32 gives a semantic name to i32. This
improves readability, but Floor is still just an i32 to the compiler.#[derive(Debug)]: We use this attribute to automatically generate code
to format the enums for printing with {:?}. Without this, we would have to
manually implement the fmt::Debug trait.Button enum is nested inside Event::ButtonPressed.
This hierarchical structure is common in Rust for modeling complex domains.Event::CarDoorOpened is a "unit variant" (it carries no data),
while Event::CarArrived is a "tuple variant".Button is a separate enum rather than just having
LobbyCallButtonPressed and CarFloorButtonPressed variants on Event. Both
are valid, but grouping related concepts (like buttons) can make the code
cleaner.