]> git.lizzy.rs Git - rust.git/blob - src/test/ui/binding/match-vec-alternatives.rs
Rollup merge of #63191 - pietroalbini:really-fix-toolstate, r=alexcrichton
[rust.git] / src / test / ui / binding / match-vec-alternatives.rs
1 // run-pass
2 #![feature(slice_patterns)]
3
4 fn match_vecs<'a, T>(l1: &'a [T], l2: &'a [T]) -> &'static str {
5     match (l1, l2) {
6         (&[], &[]) => "both empty",
7         (&[], &[..]) | (&[..], &[]) => "one empty",
8         (&[..], &[..]) => "both non-empty"
9     }
10 }
11
12 fn match_vecs_cons<'a, T>(l1: &'a [T], l2: &'a [T]) -> &'static str {
13     match (l1, l2) {
14         (&[], &[]) => "both empty",
15         (&[], &[_, ..]) | (&[_, ..], &[]) => "one empty",
16         (&[_, ..], &[_, ..]) => "both non-empty"
17     }
18 }
19
20 fn match_vecs_snoc<'a, T>(l1: &'a [T], l2: &'a [T]) -> &'static str {
21     match (l1, l2) {
22         (&[], &[]) => "both empty",
23         (&[], &[.., _]) | (&[.., _], &[]) => "one empty",
24         (&[.., _], &[.., _]) => "both non-empty"
25     }
26 }
27
28 fn match_nested_vecs_cons<'a, T>(l1: Option<&'a [T]>, l2: Result<&'a [T], ()>) -> &'static str {
29     match (l1, l2) {
30         (Some(&[]), Ok(&[])) => "Some(empty), Ok(empty)",
31         (Some(&[_, ..]), Ok(_)) | (Some(&[_, ..]), Err(())) => "Some(non-empty), any",
32         (None, Ok(&[])) | (None, Err(())) | (None, Ok(&[_])) => "None, Ok(less than one element)",
33         (None, Ok(&[_, _, ..])) => "None, Ok(at least two elements)",
34         _ => "other"
35     }
36 }
37
38 fn match_nested_vecs_snoc<'a, T>(l1: Option<&'a [T]>, l2: Result<&'a [T], ()>) -> &'static str {
39     match (l1, l2) {
40         (Some(&[]), Ok(&[])) => "Some(empty), Ok(empty)",
41         (Some(&[.., _]), Ok(_)) | (Some(&[.., _]), Err(())) => "Some(non-empty), any",
42         (None, Ok(&[])) | (None, Err(())) | (None, Ok(&[_])) => "None, Ok(less than one element)",
43         (None, Ok(&[.., _, _])) => "None, Ok(at least two elements)",
44         _ => "other"
45     }
46 }
47
48 fn main() {
49     assert_eq!(match_vecs(&[1, 2], &[2, 3]), "both non-empty");
50     assert_eq!(match_vecs(&[], &[1, 2, 3, 4]), "one empty");
51     assert_eq!(match_vecs::<usize>(&[], &[]), "both empty");
52     assert_eq!(match_vecs(&[1, 2, 3], &[]), "one empty");
53
54     assert_eq!(match_vecs_cons(&[1, 2], &[2, 3]), "both non-empty");
55     assert_eq!(match_vecs_cons(&[], &[1, 2, 3, 4]), "one empty");
56     assert_eq!(match_vecs_cons::<usize>(&[], &[]), "both empty");
57     assert_eq!(match_vecs_cons(&[1, 2, 3], &[]), "one empty");
58
59     assert_eq!(match_vecs_snoc(&[1, 2], &[2, 3]), "both non-empty");
60     assert_eq!(match_vecs_snoc(&[], &[1, 2, 3, 4]), "one empty");
61     assert_eq!(match_vecs_snoc::<usize>(&[], &[]), "both empty");
62     assert_eq!(match_vecs_snoc(&[1, 2, 3], &[]), "one empty");
63
64     assert_eq!(match_nested_vecs_cons(None, Ok::<&[_], ()>(&[4_usize, 2_usize])),
65                "None, Ok(at least two elements)");
66     assert_eq!(match_nested_vecs_cons::<usize>(None, Err(())), "None, Ok(less than one element)");
67     assert_eq!(match_nested_vecs_cons::<bool>(Some::<&[_]>(&[]), Ok::<&[_], ()>(&[])),
68                "Some(empty), Ok(empty)");
69     assert_eq!(match_nested_vecs_cons(Some::<&[_]>(&[1]), Err(())), "Some(non-empty), any");
70     assert_eq!(match_nested_vecs_cons(Some::<&[_]>(&[(42, ())]), Ok::<&[_], ()>(&[(1, ())])),
71                "Some(non-empty), any");
72
73     assert_eq!(match_nested_vecs_snoc(None, Ok::<&[_], ()>(&[4_usize, 2_usize])),
74                "None, Ok(at least two elements)");
75     assert_eq!(match_nested_vecs_snoc::<usize>(None, Err(())), "None, Ok(less than one element)");
76     assert_eq!(match_nested_vecs_snoc::<bool>(Some::<&[_]>(&[]), Ok::<&[_], ()>(&[])),
77                "Some(empty), Ok(empty)");
78     assert_eq!(match_nested_vecs_snoc(Some::<&[_]>(&[1]), Err(())), "Some(non-empty), any");
79     assert_eq!(match_nested_vecs_snoc(Some::<&[_]>(&[(42, ())]), Ok::<&[_], ()>(&[(1, ())])),
80                "Some(non-empty), any");
81 }