]> git.lizzy.rs Git - rust.git/blob - tests/ui/binding/if-let.rs
Merge commit '598f0909568a51de8a2d1148f55a644fd8dffad0' into sync_cg_clif-2023-01-24
[rust.git] / tests / ui / binding / if-let.rs
1 // run-pass
2 #![allow(dead_code)]
3
4 pub fn main() {
5     let x = Some(3);
6     if let Some(y) = x {
7         assert_eq!(y, 3);
8     } else {
9         panic!("`if let` panicked");
10     }
11     let mut worked = false;
12     if let Some(_) = x {
13         worked = true;
14     }
15     assert!(worked);
16     let clause: usize;
17     if let None = Some("test") {
18         clause = 1;
19     } else if 4_usize > 5 {
20         clause = 2;
21     } else if let Ok(()) = Err::<(),&'static str>("test") {
22         clause = 3;
23     } else {
24         clause = 4;
25     }
26     assert_eq!(clause, 4_usize);
27
28     if 3 > 4 {
29         panic!("bad math");
30     } else if let 1 = 2 {
31         panic!("bad pattern match");
32     }
33
34     enum Foo {
35         One,
36         Two(usize),
37         Three(String, isize)
38     }
39
40     let foo = Foo::Three("three".to_string(), 42);
41     if let Foo::One = foo {
42         panic!("bad pattern match");
43     } else if let Foo::Two(_x) = foo {
44         panic!("bad pattern match");
45     } else if let Foo::Three(s, _) = foo {
46         assert_eq!(s, "three");
47     } else {
48         panic!("bad else");
49     }
50
51     if false {
52         panic!("wat");
53     } else if let a@Foo::Two(_) = Foo::Two(42_usize) {
54         if let Foo::Two(b) = a {
55             assert_eq!(b, 42_usize);
56         } else {
57             panic!("panic in nested `if let`");
58         }
59     }
60 }