diff --git a/rustbyexample/custom_types/custom_types_enum.rs b/rustbyexample/custom_types/custom_types_enum.rs new file mode 100644 index 0000000..2ad692e --- /dev/null +++ b/rustbyexample/custom_types/custom_types_enum.rs @@ -0,0 +1,33 @@ +enum WebEvent { + PageLoad, + PageUnload, + KeyPress(char), + Paste(String), + Click { x: i64, y: i64 }, +} + +fn inspect(event: WebEvent) { + match event { + WebEvent::PageLoad => println!("page loaded"), + WebEvent::PageUnload => println!("page unloaded"), + WebEvent::KeyPress(c) => println!("pressed '{}'.", c), + WebEvent::Paste(s) => println!("pasted \"{}\".", s), + WebEvent::Click { x, y } => { + println!("clicked at x={}, y={}.", x, y); + } + } +} + +fn main() { + let pressed = WebEvent::KeyPress('x'); + let pasted = WebEvent::Paste("my text".to_owned()); + let click = WebEvent::Click { x: 20, y: 80 }; + let load = WebEvent::PageLoad; + let unload = WebEvent::PageUnload; + + inspect(pressed); + inspect(pasted); + inspect(click); + inspect(load); + inspect(unload); +}