summaryrefslogtreecommitdiffstats
path: root/tests/ui/pattern/bindings-after-at/slice-patterns.rs
blob: 4f4c96e450b644b7e2a3a6ec5c969b9287b550ae (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
// Test bindings-after-at with slice-patterns

// run-pass


#[derive(Debug, PartialEq)]
enum MatchArm {
    Arm(usize),
    Wild,
}

fn test(foo: &[i32]) -> MatchArm {
    match foo {
        [bar @ .., n] if n == &5 => {
            for i in bar {
                assert!(i < &5);
            }

            MatchArm::Arm(0)
        },
        bar @ [x0, .., xn] => {
            assert_eq!(x0, &1);
            assert_eq!(x0, &1);
            assert_eq!(xn, &4);
            assert_eq!(bar, &[1, 2, 3, 4]);

            MatchArm::Arm(1)
        },
        _ => MatchArm::Wild,
    }
}

fn main() {
    let foo = vec![1, 2, 3, 4, 5];

    assert_eq!(test(&foo), MatchArm::Arm(0));
    assert_eq!(test(&foo[..4]), MatchArm::Arm(1));
    assert_eq!(test(&foo[0..1]), MatchArm::Wild);
}