解答

1. #[derive(Debug)]
2. /// An event in the elevator system that the controller must react to.
3. enum Event {
4. /// A button was pressed.
5. ButtonPressed(Button),

7. /// The car has arrived at the given floor.
8. CarArrived(Floor),

10. /// The car's doors have opened.
11. CarDoorOpened,

13. /// The car's doors have closed.
14. CarDoorClosed,
15. }

17. /// A floor is represented as an integer.
18. type Floor = i32;

20. /// A direction of travel.
21. #[derive(Debug)]
22. enum Direction {
23. Up,
24. Down,
25. }

27. /// A user-accessible button.
28. #[derive(Debug)]
29. enum Button {
30. /// A button in the elevator lobby on the given floor.
31. LobbyCall(Direction, Floor),

33. /// A floor button within the car.
34. CarFloor(Floor),
35. }

37. /// The car has arrived on the given floor.
38. fn car_arrived(floor: i32) -> Event {
39. Event::CarArrived(floor)
40. }

42. /// The car doors have opened.
43. fn car_door_opened() -> Event {
44. Event::CarDoorOpened
45. }

47. /// The car doors have closed.
48. fn car_door_closed() -> Event {
49. Event::CarDoorClosed
50. }

52. /// A directional button was pressed in an elevator lobby on the given floor.
53. fn lobby_call_button_pressed(floor: i32, dir: Direction) -> Event {
54. Event::ButtonPressed(Button::LobbyCall(dir, floor))
55. }

57. /// A floor button was pressed in the elevator car.
58. fn car_floor_button_pressed(floor: i32) -> Event {
59. Event::ButtonPressed(Button::CarFloor(floor))
60. }

62. fn main() {
63. println!(
64. "A ground floor passenger has pressed the up button: {:?}",
65. lobby_call_button_pressed(0, Direction::Up)
66. );
67. println!("The car has arrived on the ground floor: {:?}", car_arrived(0));
68. println!("The car door opened: {:?}", car_door_opened());
69. println!(
70. "A passenger has pressed the 3rd floor button: {:?}",
71. car_floor_button_pressed(3)
72. );
73. println!("The car door closed: {:?}", car_door_closed());
74. println!("The car has arrived on the 3rd floor: {:?}", car_arrived(3));
75. }