From 6e50ec65f8b92cfc008564e2f503de8bea41549e Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Fri, 29 Sep 2023 16:52:18 -0400 Subject: [PATCH] Flow control match binding in RBE --- rustbyexample/flow_control.d/match.d/binding.rs | 14 ++++++++++++++ rustbyexample/flow_control.d/match.d/binding2.rs | 11 +++++++++++ 2 files changed, 25 insertions(+) create mode 100644 rustbyexample/flow_control.d/match.d/binding.rs create mode 100644 rustbyexample/flow_control.d/match.d/binding2.rs diff --git a/rustbyexample/flow_control.d/match.d/binding.rs b/rustbyexample/flow_control.d/match.d/binding.rs new file mode 100644 index 0000000..f544c71 --- /dev/null +++ b/rustbyexample/flow_control.d/match.d/binding.rs @@ -0,0 +1,14 @@ +fn age() -> u32 { + 15 +} + +fn main() { + println!("Tell me what type of person you are"); + + match age() { + 0 => println!("I haven't celebrated my first birthday yet"), + n @ 1..=12 => println!("I'm a child of age {:?}", n), + n @ 13..=19 => println!("I'm a teen of age {:?}", n), + n => println!("I'm an old person of age {:?}", n), + } +} diff --git a/rustbyexample/flow_control.d/match.d/binding2.rs b/rustbyexample/flow_control.d/match.d/binding2.rs new file mode 100644 index 0000000..022b642 --- /dev/null +++ b/rustbyexample/flow_control.d/match.d/binding2.rs @@ -0,0 +1,11 @@ +fn some_number() -> Option { + Some(42) +} + +fn main() { + match some_number() { + Some(n @ 42) => println!("The Answer: {}!", n), + Some(n) => println!("Not interesting... {}", n), + _ => (), + } +}