-
Notifications
You must be signed in to change notification settings - Fork 32
/
bind-by-copy.rs
90 lines (80 loc) · 1.81 KB
/
bind-by-copy.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//@ run-pass
#![allow(unused)]
// Test copy
struct A {
a: i32,
b: i32,
}
struct B {
a: i32,
b: C,
}
struct D {
a: i32,
d: C,
}
#[derive(Copy, Clone)]
struct C {
c: i32,
}
enum E {
E { a: i32, e: C },
NotE,
}
#[rustfmt::skip]
pub fn main() {
match (A {a: 10, b: 20}) {
x@A {a, b: 20} => { assert!(x.a == 10); assert!(a == 10); }
A {b: _b, ..} => { panic!(); }
}
let mut x@B {b, ..} = B {a: 10, b: C {c: 20}};
assert_eq!(x.a, 10);
x.b.c = 30;
assert_eq!(b.c, 20);
let mut y@D {d, ..} = D {a: 10, d: C {c: 20}};
assert_eq!(y.a, 10);
y.d.c = 30;
assert_eq!(d.c, 20);
match (E::E { a: 10, e: C { c: 20 } }) {
x @ E::E{ a, e: C { c } } => {
assert!(matches!(x, E::E { a: 10, e: C { c: 20 } }));
assert!(a == 10);
assert!(c == 20);
}
_ => panic!(),
}
match (E::E { a: 10, e: C { c: 20 } }) {
mut x @ E::E{ a, e: C { mut c } } => {
x = E::NotE;
c += 30;
assert_eq!(c, 50);
}
_ => panic!(),
}
let some_b = Some(B { a: 10, b: C { c: 20 } });
// in irrefutable pattern
if let Some(x @ B { b, .. }) = some_b {
assert_eq!(x.b.c, 20);
assert_eq!(b.c, 20);
} else {
unreachable!();
}
let some_b = Some(B { a: 10, b: C { c: 20 } });
if let Some(x @ B { b: mut b @ C { c }, .. }) = some_b {
assert_eq!(x.b.c, 20);
assert_eq!(b.c, 20);
b.c = 30;
assert_eq!(b.c, 30);
assert_eq!(c, 20);
} else {
unreachable!();
}
}
// ferrocene-annotations: fls_nruvg0es3kx7
// Record Struct Patterns
//
// ferrocene-annotations: fls_asj8rgccvkoe
// Struct Pattern Matching
//
// ferrocene-annotations: fls_7dbd5t2750ce
// Struct Patterns