summaryrefslogtreecommitdiffstats
path: root/tests/ui/let-else/let-else-bindings.rs
blob: 53ac398b8f5231c9f98c326742d1a6f663407c33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// run-pass
// adapted from tests/ui/binding/if-let.rs

#![allow(dead_code)]

fn none() -> bool {
    let None = Some("test") else {
        return true;
    };
    false
}

fn ok() -> bool {
    let Ok(()) = Err::<(),&'static str>("test") else {
        return true;
    };
    false
}

pub fn main() {
    let x = Some(3);
    let Some(y) = x else {
        panic!("let-else panicked");
    };
    assert_eq!(y, 3);
    let Some(_) = x else {
        panic!("bad match");
    };
    assert!(none());
    assert!(ok());

    assert!((|| {
        let 1 = 2 else {
            return true;
        };
        false
    })());

    enum Foo {
        One,
        Two(usize),
        Three(String, isize),
    }

    let foo = Foo::Three("three".to_string(), 42);
    let one = || {
        let Foo::One = foo else {
            return true;
        };
        false
    };
    assert!(one());
    let two = || {
        let Foo::Two(_x) = foo else {
            return true;
        };
        false
    };
    assert!(two());
    let three = || {
        let Foo::Three(s, _x) = foo else {
            return false;
        };
        s == "three"
    };
    assert!(three());

    let a@Foo::Two(_) = Foo::Two(42_usize) else {
        panic!("bad match")
    };
    let Foo::Two(b) = a else {
        panic!("panic in nested `if let`");
    };
    assert_eq!(b, 42_usize);
}