]> git.lizzy.rs Git - rust.git/blob - tests/ui/array-slice-vec/vec-matching.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / array-slice-vec / vec-matching.rs
1 // run-pass
2
3 fn a() {
4     let x = [1];
5     match x {
6         [a] => {
7             assert_eq!(a, 1);
8         }
9     }
10 }
11
12 fn b() {
13     let x = [1, 2, 3];
14     match x {
15         [a, b, c @ ..] => {
16             assert_eq!(a, 1);
17             assert_eq!(b, 2);
18             let expected: &[_] = &[3];
19             assert_eq!(c, expected);
20         }
21     }
22     match x {
23         [a @ .., b, c] => {
24             let expected: &[_] = &[1];
25             assert_eq!(a, expected);
26             assert_eq!(b, 2);
27             assert_eq!(c, 3);
28         }
29     }
30     match x {
31         [a, b @ .., c] => {
32             assert_eq!(a, 1);
33             let expected: &[_] = &[2];
34             assert_eq!(b, expected);
35             assert_eq!(c, 3);
36         }
37     }
38     match x {
39         [a, b, c] => {
40             assert_eq!(a, 1);
41             assert_eq!(b, 2);
42             assert_eq!(c, 3);
43         }
44     }
45 }
46
47
48 fn b_slice() {
49     let x : &[_] = &[1, 2, 3];
50     match x {
51         &[a, b, ref c @ ..] => {
52             assert_eq!(a, 1);
53             assert_eq!(b, 2);
54             let expected: &[_] = &[3];
55             assert_eq!(c, expected);
56         }
57         _ => unreachable!()
58     }
59     match x {
60         &[ref a @ .., b, c] => {
61             let expected: &[_] = &[1];
62             assert_eq!(a, expected);
63             assert_eq!(b, 2);
64             assert_eq!(c, 3);
65         }
66         _ => unreachable!()
67     }
68     match x {
69         &[a, ref b @ .., c] => {
70             assert_eq!(a, 1);
71             let expected: &[_] = &[2];
72             assert_eq!(b, expected);
73             assert_eq!(c, 3);
74         }
75         _ => unreachable!()
76     }
77     match x {
78         &[a, b, c] => {
79             assert_eq!(a, 1);
80             assert_eq!(b, 2);
81             assert_eq!(c, 3);
82         }
83         _ => unreachable!()
84     }
85 }
86
87 fn c() {
88     let x = [1];
89     match x {
90         [2, ..] => panic!(),
91         [..] => ()
92     }
93 }
94
95 fn d() {
96     let x = [1, 2, 3];
97     let branch = match x {
98         [1, 1, ..] => 0,
99         [1, 2, 3, ..] => 1,
100         [1, 2, ..] => 2,
101         _ => 3
102     };
103     assert_eq!(branch, 1);
104 }
105
106 fn e() {
107     let x: &[isize] = &[1, 2, 3];
108     let a = match *x {
109         [1, 2] => 0,
110         [..] => 1,
111     };
112
113     assert_eq!(a, 1);
114
115     let b = match *x {
116         [2, ..] => 0,
117         [1, 2, ..] => 1,
118         [_] => 2,
119         [..] => 3
120     };
121
122     assert_eq!(b, 1);
123
124
125     let c = match *x {
126         [_, _, _, _, ..] => 0,
127         [1, 2, ..] => 1,
128         [_] => 2,
129         [..] => 3
130     };
131
132     assert_eq!(c, 1);
133 }
134
135 pub fn main() {
136     a();
137     b();
138     b_slice();
139     c();
140     d();
141     e();
142 }