]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/vec-matching.rs
Rollup merge of #34175 - rwz:patch-2, r=alexcrichton
[rust.git] / src / test / run-pass / vec-matching.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11
12 #![feature(advanced_slice_patterns)]
13 #![feature(slice_patterns)]
14 #![feature(rustc_attrs)]
15
16 #[rustc_mir]
17 fn a() {
18     let x = [1];
19     match x {
20         [a] => {
21             assert_eq!(a, 1);
22         }
23     }
24 }
25
26 #[rustc_mir]
27 fn b() {
28     let x = [1, 2, 3];
29     match x {
30         [a, b, c..] => {
31             assert_eq!(a, 1);
32             assert_eq!(b, 2);
33             let expected: &[_] = &[3];
34             assert_eq!(c, expected);
35         }
36     }
37     match x {
38         [a.., b, c] => {
39             let expected: &[_] = &[1];
40             assert_eq!(a, expected);
41             assert_eq!(b, 2);
42             assert_eq!(c, 3);
43         }
44     }
45     match x {
46         [a, b.., c] => {
47             assert_eq!(a, 1);
48             let expected: &[_] = &[2];
49             assert_eq!(b, expected);
50             assert_eq!(c, 3);
51         }
52     }
53     match x {
54         [a, b, c] => {
55             assert_eq!(a, 1);
56             assert_eq!(b, 2);
57             assert_eq!(c, 3);
58         }
59     }
60 }
61
62
63 #[rustc_mir]
64 fn b_slice() {
65     let x : &[_] = &[1, 2, 3];
66     match x {
67         &[a, b, ref c..] => {
68             assert_eq!(a, 1);
69             assert_eq!(b, 2);
70             let expected: &[_] = &[3];
71             assert_eq!(c, expected);
72         }
73         _ => unreachable!()
74     }
75     match x {
76         &[ref a.., b, c] => {
77             let expected: &[_] = &[1];
78             assert_eq!(a, expected);
79             assert_eq!(b, 2);
80             assert_eq!(c, 3);
81         }
82         _ => unreachable!()
83     }
84     match x {
85         &[a, ref b.., c] => {
86             assert_eq!(a, 1);
87             let expected: &[_] = &[2];
88             assert_eq!(b, expected);
89             assert_eq!(c, 3);
90         }
91         _ => unreachable!()
92     }
93     match x {
94         &[a, b, c] => {
95             assert_eq!(a, 1);
96             assert_eq!(b, 2);
97             assert_eq!(c, 3);
98         }
99         _ => unreachable!()
100     }
101 }
102
103 #[rustc_mir]
104 fn c() {
105     let x = [1];
106     match x {
107         [2, ..] => panic!(),
108         [..] => ()
109     }
110 }
111
112 #[rustc_mir]
113 fn d() {
114     let x = [1, 2, 3];
115     let branch = match x {
116         [1, 1, ..] => 0,
117         [1, 2, 3, ..] => 1,
118         [1, 2, ..] => 2,
119         _ => 3
120     };
121     assert_eq!(branch, 1);
122 }
123
124 #[rustc_mir]
125 fn e() {
126     let x: &[isize] = &[1, 2, 3];
127     let a = match *x {
128         [1, 2] => 0,
129         [..] => 1,
130     };
131
132     assert_eq!(a, 1);
133
134     let b = match *x {
135         [2, ..] => 0,
136         [1, 2, ..] => 1,
137         [_] => 2,
138         [..] => 3
139     };
140
141     assert_eq!(b, 1);
142
143
144     let c = match *x {
145         [_, _, _, _, ..] => 0,
146         [1, 2, ..] => 1,
147         [_] => 2,
148         [..] => 3
149     };
150
151     assert_eq!(c, 1);
152 }
153
154 pub fn main() {
155     a();
156     b();
157     b_slice();
158     c();
159     d();
160     e();
161 }