Skip to content

Instantly share code, notes, and snippets.

@timClicks
Created June 11, 2023 21:26
Show Gist options
  • Save timClicks/15fa583b20a3e5272bb0cfc269c7b4c0 to your computer and use it in GitHub Desktop.
Save timClicks/15fa583b20a3e5272bb0cfc269c7b4c0 to your computer and use it in GitHub Desktop.
A simple finite state machine in Rust (from https://youtu.be/xuu3FrTKb50)
#[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