2018-08-30 12:18:55 +00:00
|
|
|
//@ run-pass
|
2018-09-25 21:51:35 +00:00
|
|
|
#![allow(dead_code)]
|
2015-03-22 20:13:15 +00:00
|
|
|
|
2014-08-25 05:08:13 +00:00
|
|
|
pub fn main() {
|
2015-01-25 21:05:03 +00:00
|
|
|
let x = Some(3);
|
2014-08-25 05:08:13 +00:00
|
|
|
if let Some(y) = x {
|
2015-01-25 21:05:03 +00:00
|
|
|
assert_eq!(y, 3);
|
2014-08-25 05:08:13 +00:00
|
|
|
} else {
|
2021-02-17 10:26:38 +00:00
|
|
|
panic!("`if let` panicked");
|
2014-08-25 05:08:13 +00:00
|
|
|
}
|
|
|
|
let mut worked = false;
|
|
|
|
if let Some(_) = x {
|
|
|
|
worked = true;
|
|
|
|
}
|
|
|
|
assert!(worked);
|
2015-03-26 00:06:52 +00:00
|
|
|
let clause: usize;
|
2014-08-25 05:08:13 +00:00
|
|
|
if let None = Some("test") {
|
|
|
|
clause = 1;
|
2015-02-18 10:42:01 +00:00
|
|
|
} else if 4_usize > 5 {
|
2014-08-25 05:08:13 +00:00
|
|
|
clause = 2;
|
|
|
|
} else if let Ok(()) = Err::<(),&'static str>("test") {
|
|
|
|
clause = 3;
|
|
|
|
} else {
|
|
|
|
clause = 4;
|
|
|
|
}
|
2015-02-18 10:42:01 +00:00
|
|
|
assert_eq!(clause, 4_usize);
|
2014-08-25 05:08:13 +00:00
|
|
|
|
2015-01-25 21:05:03 +00:00
|
|
|
if 3 > 4 {
|
2014-10-09 19:17:22 +00:00
|
|
|
panic!("bad math");
|
2015-01-25 21:05:03 +00:00
|
|
|
} else if let 1 = 2 {
|
2014-10-09 19:17:22 +00:00
|
|
|
panic!("bad pattern match");
|
2014-08-25 05:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum Foo {
|
|
|
|
One,
|
2015-03-26 00:06:52 +00:00
|
|
|
Two(usize),
|
|
|
|
Three(String, isize)
|
2014-08-25 05:08:13 +00:00
|
|
|
}
|
|
|
|
|
2015-01-25 21:05:03 +00:00
|
|
|
let foo = Foo::Three("three".to_string(), 42);
|
2014-11-06 08:05:53 +00:00
|
|
|
if let Foo::One = foo {
|
2014-10-09 19:17:22 +00:00
|
|
|
panic!("bad pattern match");
|
2014-11-06 08:05:53 +00:00
|
|
|
} else if let Foo::Two(_x) = foo {
|
2014-10-09 19:17:22 +00:00
|
|
|
panic!("bad pattern match");
|
2014-11-06 08:05:53 +00:00
|
|
|
} else if let Foo::Three(s, _) = foo {
|
2015-02-02 02:53:25 +00:00
|
|
|
assert_eq!(s, "three");
|
2014-08-25 05:08:13 +00:00
|
|
|
} else {
|
2014-10-09 19:17:22 +00:00
|
|
|
panic!("bad else");
|
2014-08-25 05:08:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if false {
|
2014-10-09 19:17:22 +00:00
|
|
|
panic!("wat");
|
2015-02-18 10:42:01 +00:00
|
|
|
} else if let a@Foo::Two(_) = Foo::Two(42_usize) {
|
2014-11-06 08:05:53 +00:00
|
|
|
if let Foo::Two(b) = a {
|
2015-02-18 10:42:01 +00:00
|
|
|
assert_eq!(b, 42_usize);
|
2014-08-25 05:08:13 +00:00
|
|
|
} else {
|
2021-02-17 10:26:38 +00:00
|
|
|
panic!("panic in nested `if let`");
|
2014-08-25 05:08:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|