练习:电梯事件

我们将创建一个数据结构来表示电梯控制系统中的事件。您可以自行定义用于构造各种事件的类型和函数。使用 #[derive(Debug)] 以允许通过 {:?} 设置类型格式。

This exercise only requires creating and populating data structures so that main runs without errors. The next part of the course will cover getting data out of these structures.

1. #[derive(Debug)]
2. /// An event in the elevator system that the controller must react to.
3. enum Event {
4. // TODO: add required variants
5. }

7. /// A direction of travel.
8. #[derive(Debug)]
9. enum Direction {
10. Up,
11. Down,
12. }

14. /// The car has arrived on the given floor.
15. fn car_arrived(floor: i32) -> Event {
16. todo!()
17. }

19. /// The car doors have opened.
20. fn car_door_opened() -> Event {
21. todo!()
22. }

24. /// The car doors have closed.
25. fn car_door_closed() -> Event {
26. todo!()
27. }

29. /// A directional button was pressed in an elevator lobby on the given floor.
30. fn lobby_call_button_pressed(floor: i32, dir: Direction) -> Event {
31. todo!()
32. }

34. /// A floor button was pressed in the elevator car.
35. fn car_floor_button_pressed(floor: i32) -> Event {
36. todo!()
37. }

39. fn main() {
40. println!(
41. "A ground floor passenger has pressed the up button: {:?}",
42. lobby_call_button_pressed(0, Direction::Up)
43. );
44. println!("The car has arrived on the ground floor: {:?}", car_arrived(0));
45. println!("The car door opened: {:?}", car_door_opened());
46. println!(
47. "A passenger has pressed the 3rd floor button: {:?}",
48. car_floor_button_pressed(3)
49. );
50. println!("The car door closed: {:?}", car_door_closed());
51. println!("The car has arrived on the 3rd floor: {:?}", car_arrived(3));
52. }