]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/match-vec-alternatives.rs
Fix BTreeMap example typo
[rust.git] / src / test / run-pass / match-vec-alternatives.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
15 fn match_vecs<'a, T>(l1: &'a [T], l2: &'a [T]) -> &'static str {
16     match (l1, l2) {
17         ([], []) => "both empty",
18         ([], [..]) | ([..], []) => "one empty",
19         ([..], [..]) => "both non-empty"
20     }
21 }
22
23 fn match_vecs_cons<'a, T>(l1: &'a [T], l2: &'a [T]) -> &'static str {
24     match (l1, l2) {
25         ([], []) => "both empty",
26         ([], [_, ..]) | ([_, ..], []) => "one empty",
27         ([_, ..], [_, ..]) => "both non-empty"
28     }
29 }
30
31 fn match_vecs_snoc<'a, T>(l1: &'a [T], l2: &'a [T]) -> &'static str {
32     match (l1, l2) {
33         ([], []) => "both empty",
34         ([], [.., _]) | ([.., _], []) => "one empty",
35         ([.., _], [.., _]) => "both non-empty"
36     }
37 }
38
39 fn match_nested_vecs_cons<'a, T>(l1: Option<&'a [T]>, l2: Result<&'a [T], ()>) -> &'static str {
40     match (l1, l2) {
41         (Some([]), Ok([])) => "Some(empty), Ok(empty)",
42         (Some([_, ..]), Ok(_)) | (Some([_, ..]), Err(())) => "Some(non-empty), any",
43         (None, Ok([])) | (None, Err(())) | (None, Ok([_])) => "None, Ok(less than one element)",
44         (None, Ok([_, _, ..])) => "None, Ok(at least two elements)",
45         _ => "other"
46     }
47 }
48
49 fn match_nested_vecs_snoc<'a, T>(l1: Option<&'a [T]>, l2: Result<&'a [T], ()>) -> &'static str {
50     match (l1, l2) {
51         (Some([]), Ok([])) => "Some(empty), Ok(empty)",
52         (Some([.., _]), Ok(_)) | (Some([.., _]), Err(())) => "Some(non-empty), any",
53         (None, Ok([])) | (None, Err(())) | (None, Ok([_])) => "None, Ok(less than one element)",
54         (None, Ok([.., _, _])) => "None, Ok(at least two elements)",
55         _ => "other"
56     }
57 }
58
59 fn main() {
60     assert_eq!(match_vecs(&[1, 2], &[2, 3]), "both non-empty");
61     assert_eq!(match_vecs(&[], &[1, 2, 3, 4]), "one empty");
62     assert_eq!(match_vecs::<usize>(&[], &[]), "both empty");
63     assert_eq!(match_vecs(&[1, 2, 3], &[]), "one empty");
64
65     assert_eq!(match_vecs_cons(&[1, 2], &[2, 3]), "both non-empty");
66     assert_eq!(match_vecs_cons(&[], &[1, 2, 3, 4]), "one empty");
67     assert_eq!(match_vecs_cons::<usize>(&[], &[]), "both empty");
68     assert_eq!(match_vecs_cons(&[1, 2, 3], &[]), "one empty");
69
70     assert_eq!(match_vecs_snoc(&[1, 2], &[2, 3]), "both non-empty");
71     assert_eq!(match_vecs_snoc(&[], &[1, 2, 3, 4]), "one empty");
72     assert_eq!(match_vecs_snoc::<usize>(&[], &[]), "both empty");
73     assert_eq!(match_vecs_snoc(&[1, 2, 3], &[]), "one empty");
74
75     assert_eq!(match_nested_vecs_cons(None, Ok::<&[_], ()>(&[4_usize, 2_usize])),
76                "None, Ok(at least two elements)");
77     assert_eq!(match_nested_vecs_cons::<usize>(None, Err(())), "None, Ok(less than one element)");
78     assert_eq!(match_nested_vecs_cons::<bool>(Some::<&[_]>(&[]), Ok::<&[_], ()>(&[])),
79                "Some(empty), Ok(empty)");
80     assert_eq!(match_nested_vecs_cons(Some::<&[_]>(&[1]), Err(())), "Some(non-empty), any");
81     assert_eq!(match_nested_vecs_cons(Some::<&[_]>(&[(42, ())]), Ok::<&[_], ()>(&[(1, ())])),
82                "Some(non-empty), any");
83
84     assert_eq!(match_nested_vecs_snoc(None, Ok::<&[_], ()>(&[4_usize, 2_usize])),
85                "None, Ok(at least two elements)");
86     assert_eq!(match_nested_vecs_snoc::<usize>(None, Err(())), "None, Ok(less than one element)");
87     assert_eq!(match_nested_vecs_snoc::<bool>(Some::<&[_]>(&[]), Ok::<&[_], ()>(&[])),
88                "Some(empty), Ok(empty)");
89     assert_eq!(match_nested_vecs_snoc(Some::<&[_]>(&[1]), Err(())), "Some(non-empty), any");
90     assert_eq!(match_nested_vecs_snoc(Some::<&[_]>(&[(42, ())]), Ok::<&[_], ()>(&[(1, ())])),
91                "Some(non-empty), any");
92 }