-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathfinite-state-machine.rs
60 lines (55 loc) · 1.36 KB
/
finite-state-machine.rs
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
//! A basic traffic light state machine.
//!
//! Wikipedia definition of a FSM:
//! A finite state machine can be in exactly one of a finite number of states at any given time.
//! The FSM can change from one state to another in response to some inputs; the change from one state to another is called a transition.
//! An FSM is defined by a list of its states, its initial state, and the inputs that trigger each transition.
//!
//! Tested with rust-1.6.4
//!
//! @license MIT license <http://www.opensource.org/licenses/mit-license.php>
//!
//! @since 2023-01-05
#[derive(Debug)]
enum TrafficLight {
Green,
Yellow,
Red,
}
impl TrafficLight {
pub fn new() -> Self {
Self::Green
}
pub fn next(&self) -> Self {
match self {
Self::Green => Self::Yellow,
Self::Yellow => Self::Red,
Self::Red => Self::Green,
}
}
}
#[derive(Debug)]
struct Crosswalk {
light: TrafficLight,
}
impl Crosswalk {
pub fn new() -> Self {
Crosswalk {
light: TrafficLight::new(),
}
}
pub fn next(&mut self) {
self.light = self.light.next();
self.print();
}
pub fn print(&self) {
println!("{:?}", self);
}
}
fn main() {
let mut crosswalk = Crosswalk::new();
crosswalk.print();
crosswalk.next();
crosswalk.next();
crosswalk.next();
}