Created
June 11, 2023 21:26
-
-
Save timClicks/15fa583b20a3e5272bb0cfc269c7b4c0 to your computer and use it in GitHub Desktop.
A simple finite state machine in Rust (from https://youtu.be/xuu3FrTKb50)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug)] | |
enum TrafficSignal { | |
Green, | |
Yellow, | |
Red, | |
} | |
impl TrafficSignal { | |
fn step(&mut self) { | |
use TrafficSignal::*; | |
*self = match self { | |
Green => Yellow, | |
Yellow => Red, | |
Red => Green, | |
} | |
} | |
} | |
fn main() { | |
let mut light = TrafficSignal::Green; | |
light.step(); | |
light.step(); | |
println!("{light:?}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment