Some if-let in RBE

This commit is contained in:
Dan Buch 2023-09-30 19:39:55 -04:00
parent 6e50ec65f8
commit dacbddca6e
Signed by: meatballhat
GPG Key ID: A12F782281063434
3 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,25 @@
fn main() {
let number = Some(7);
let letter: Option<i32> = None;
let emoticon: Option<i32> = 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 :)!");
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,11 @@
enum Foo {
Bar,
}
fn main() {
let a = Foo::Bar;
if let Foo::Bar = a {
println!("a is foobar");
}
}