1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
| // Copyright 2023 Google LLC
// SPDX-License-Identifier: Apache-2.0
#![allow(dead_code)]
#[derive(Debug)]
/// 电梯系统中控制器必须响应的事件。
enum Event {
/// 按下了某个按钮。
ButtonPressed(Button),
/// 轿厢已到达指定楼层。
CarArrived(Floor),
/// 轿厢门已打开。
CarDoorOpened,
/// 轿厢门已关闭。
CarDoorClosed,
}
/// 楼层用整数表示。
type Floor = i32;
/// 行进方向。
#[derive(Debug)]
enum Direction {
Up,
Down,
}
/// 用户可操作的按钮。
#[derive(Debug)]
enum Button {
/// 指定楼层电梯厅中的按钮。
LobbyCall(Direction, Floor),
/// 轿厢内的楼层按钮。
CarFloor(Floor),
}
/// 轿厢已到达指定楼层。
fn car_arrived(floor: i32) -> Event {
Event::CarArrived(floor)
}
/// 轿厢门已打开。
fn car_door_opened() -> Event {
Event::CarDoorOpened
}
/// 轿厢门已关闭。
fn car_door_closed() -> Event {
Event::CarDoorClosed
}
/// 某楼层电梯厅按下了方向按钮。
fn lobby_call_button_pressed(floor: i32, dir: Direction) -> Event {
Event::ButtonPressed(Button::LobbyCall(dir, floor))
}
/// 轿厢内按下了楼层按钮。
fn car_floor_button_pressed(floor: i32) -> Event {
Event::ButtonPressed(Button::CarFloor(floor))
}
fn main() {
println!(
"A ground floor passenger has pressed the up button: {:?}",
lobby_call_button_pressed(0, Direction::Up)
);
println!("The car has arrived on the ground floor: {:?}", car_arrived(0));
println!("The car door opened: {:?}", car_door_opened());
println!(
"A passenger has pressed the 3rd floor button: {:?}",
car_floor_button_pressed(3)
);
println!("The car door closed: {:?}", car_door_closed());
println!("The car has arrived on the 3rd floor: {:?}", car_arrived(3));
}
|