From dacbddca6eddff344052916cfcc6dbe84edf7f4a Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 30 Sep 2023 19:39:55 -0400 Subject: [PATCH] Some if-let in RBE --- rustbyexample/flow_control.d/if_let.rs | 25 +++++++++++++++++++++++ rustbyexample/flow_control.d/if_let2.rs | 27 +++++++++++++++++++++++++ rustbyexample/flow_control.d/if_let3.rs | 11 ++++++++++ 3 files changed, 63 insertions(+) create mode 100644 rustbyexample/flow_control.d/if_let.rs create mode 100644 rustbyexample/flow_control.d/if_let2.rs create mode 100644 rustbyexample/flow_control.d/if_let3.rs diff --git a/rustbyexample/flow_control.d/if_let.rs b/rustbyexample/flow_control.d/if_let.rs new file mode 100644 index 0000000..63d8eb2 --- /dev/null +++ b/rustbyexample/flow_control.d/if_let.rs @@ -0,0 +1,25 @@ +fn main() { + let number = Some(7); + let letter: Option = None; + let emoticon: Option = None; + + if let Some(i) = number { + println!("Matched {:?}!", i); + } + + if let Some(i) = letter { + println!("Matched {:?}!", i); + } else { + println!("Didn't match a number. Let's go with a letter!"); + } + + let i_like_letters = false; + + if let Some(i) = emoticon { + println!("Matched {:?}!", i); + } else if i_like_letters { + println!("Didn't match a number. Let's go with a letter!"); + } else { + println!("I don't like letters. Let's go with an emoticon :)!"); + } +} diff --git a/rustbyexample/flow_control.d/if_let2.rs b/rustbyexample/flow_control.d/if_let2.rs new file mode 100644 index 0000000..e057e59 --- /dev/null +++ b/rustbyexample/flow_control.d/if_let2.rs @@ -0,0 +1,27 @@ +enum Foo { + Bar, + Baz, + Qux(u32), +} + +fn main() { + let a = Foo::Bar; + let b = Foo::Baz; + let c = Foo::Qux(100); + + if let Foo::Bar = a { + println!("a is a foobar"); + } + + if let Foo::Bar = b { + println!("b is foobar"); + } + + if let Foo::Qux(value) = c { + println!("c is {}", value); + } + + if let Foo::Qux(value @ 100) = c { + println!("c is one hundred (value: {:?})", value); + } +} diff --git a/rustbyexample/flow_control.d/if_let3.rs b/rustbyexample/flow_control.d/if_let3.rs new file mode 100644 index 0000000..07cd50e --- /dev/null +++ b/rustbyexample/flow_control.d/if_let3.rs @@ -0,0 +1,11 @@ +enum Foo { + Bar, +} + +fn main() { + let a = Foo::Bar; + + if let Foo::Bar = a { + println!("a is foobar"); + } +}