]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/vec-matching.rs
New upstream version 1.14.0+dfsg1
[rustc.git] / src / test / run-pass / vec-matching.rs
CommitLineData
1a4d82fc
JJ
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
c34b1796 11
1a4d82fc 12#![feature(advanced_slice_patterns)]
c34b1796 13#![feature(slice_patterns)]
1a4d82fc 14
223e47cc 15fn a() {
85aaf69f 16 let x = [1];
223e47cc 17 match x {
223e47cc 18 [a] => {
970d7e83 19 assert_eq!(a, 1);
223e47cc 20 }
223e47cc
LB
21 }
22}
23
24fn b() {
85aaf69f 25 let x = [1, 2, 3];
223e47cc 26 match x {
1a4d82fc 27 [a, b, c..] => {
970d7e83
LB
28 assert_eq!(a, 1);
29 assert_eq!(b, 2);
1a4d82fc
JJ
30 let expected: &[_] = &[3];
31 assert_eq!(c, expected);
223e47cc 32 }
223e47cc
LB
33 }
34 match x {
1a4d82fc
JJ
35 [a.., b, c] => {
36 let expected: &[_] = &[1];
37 assert_eq!(a, expected);
970d7e83
LB
38 assert_eq!(b, 2);
39 assert_eq!(c, 3);
223e47cc 40 }
223e47cc
LB
41 }
42 match x {
1a4d82fc 43 [a, b.., c] => {
970d7e83 44 assert_eq!(a, 1);
1a4d82fc
JJ
45 let expected: &[_] = &[2];
46 assert_eq!(b, expected);
970d7e83 47 assert_eq!(c, 3);
223e47cc 48 }
223e47cc
LB
49 }
50 match x {
51 [a, b, c] => {
970d7e83
LB
52 assert_eq!(a, 1);
53 assert_eq!(b, 2);
54 assert_eq!(c, 3);
223e47cc 55 }
1a4d82fc
JJ
56 }
57}
58
3157f602 59
3157f602
XL
60fn b_slice() {
61 let x : &[_] = &[1, 2, 3];
62 match x {
63 &[a, b, ref c..] => {
64 assert_eq!(a, 1);
65 assert_eq!(b, 2);
66 let expected: &[_] = &[3];
67 assert_eq!(c, expected);
68 }
69 _ => unreachable!()
70 }
71 match x {
72 &[ref a.., b, c] => {
73 let expected: &[_] = &[1];
74 assert_eq!(a, expected);
75 assert_eq!(b, 2);
76 assert_eq!(c, 3);
77 }
78 _ => unreachable!()
79 }
80 match x {
81 &[a, ref b.., c] => {
82 assert_eq!(a, 1);
83 let expected: &[_] = &[2];
84 assert_eq!(b, expected);
85 assert_eq!(c, 3);
86 }
87 _ => unreachable!()
88 }
89 match x {
90 &[a, b, c] => {
91 assert_eq!(a, 1);
92 assert_eq!(b, 2);
93 assert_eq!(c, 3);
94 }
95 _ => unreachable!()
96 }
97}
98
1a4d82fc 99fn c() {
85aaf69f 100 let x = [1];
1a4d82fc
JJ
101 match x {
102 [2, ..] => panic!(),
103 [..] => ()
104 }
105}
106
107fn d() {
85aaf69f 108 let x = [1, 2, 3];
1a4d82fc 109 let branch = match x {
85aaf69f
SL
110 [1, 1, ..] => 0,
111 [1, 2, 3, ..] => 1,
112 [1, 2, ..] => 2,
1a4d82fc
JJ
113 _ => 3
114 };
115 assert_eq!(branch, 1);
116}
117
118fn e() {
c34b1796 119 let x: &[isize] = &[1, 2, 3];
3157f602
XL
120 let a = match *x {
121 [1, 2] => 0,
122 [..] => 1,
123 };
124
125 assert_eq!(a, 1);
126
127 let b = match *x {
128 [2, ..] => 0,
129 [1, 2, ..] => 1,
130 [_] => 2,
131 [..] => 3
132 };
133
134 assert_eq!(b, 1);
135
136
137 let c = match *x {
138 [_, _, _, _, ..] => 0,
139 [1, 2, ..] => 1,
140 [_] => 2,
141 [..] => 3
142 };
143
144 assert_eq!(c, 1);
223e47cc
LB
145}
146
c30ab7b3
SL
147fn f() {
148 let x = &[1, 2, 3, 4, 5];
149 let [a, [b, [c, ..].., d].., e] = *x;
150 assert_eq!((a, b, c, d, e), (1, 2, 3, 4, 5));
151
152 let x: &[isize] = x;
153 let (a, b, c, d, e) = match *x {
154 [a, [b, [c, ..].., d].., e] => (a, b, c, d, e),
155 _ => unimplemented!()
156 };
157
158 assert_eq!((a, b, c, d, e), (1, 2, 3, 4, 5));
159}
160
223e47cc
LB
161pub fn main() {
162 a();
163 b();
3157f602 164 b_slice();
1a4d82fc
JJ
165 c();
166 d();
167 e();
c30ab7b3 168 f();
223e47cc 169}