rust/tests/ui/binding/if-let.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

61 lines
1.3 KiB
Rust
Raw Normal View History

//@ run-pass
#![allow(dead_code)]
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 {
panic!("`if let` panicked");
2014-08-25 05:08:13 +00:00
}
let mut worked = false;
if let Some(_) = x {
worked = true;
}
assert!(worked);
let clause: usize;
2014-08-25 05:08:13 +00:00
if let None = Some("test") {
clause = 1;
} 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;
}
assert_eq!(clause, 4_usize);
2014-08-25 05:08:13 +00:00
2015-01-25 21:05:03 +00:00
if 3 > 4 {
panic!("bad math");
2015-01-25 21:05:03 +00:00
} else if let 1 = 2 {
panic!("bad pattern match");
2014-08-25 05:08:13 +00:00
}
enum Foo {
One,
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);
if let Foo::One = foo {
panic!("bad pattern match");
} else if let Foo::Two(_x) = foo {
panic!("bad pattern match");
} else if let Foo::Three(s, _) = foo {
assert_eq!(s, "three");
2014-08-25 05:08:13 +00:00
} else {
panic!("bad else");
2014-08-25 05:08:13 +00:00
}
if false {
panic!("wat");
} else if let a@Foo::Two(_) = Foo::Two(42_usize) {
if let Foo::Two(b) = a {
assert_eq!(b, 42_usize);
2014-08-25 05:08:13 +00:00
} else {
panic!("panic in nested `if let`");
2014-08-25 05:08:13 +00:00
}
}
}