-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathclosure_capture.rs
48 lines (43 loc) · 1.12 KB
/
closure_capture.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
//@ run-pass
#![feature(deref_patterns)]
#![allow(incomplete_features)]
fn main() {
let b = Box::new("aaa".to_string());
let f = || {
let deref!(ref s) = b;
assert_eq!(s.len(), 3);
};
assert_eq!(b.len(), 3);
f();
let v = vec![1, 2, 3];
let f = || {
// this should count as a borrow of `v` as a whole
let [.., x] = v else { unreachable!() };
assert_eq!(x, 3);
};
assert_eq!(v, [1, 2, 3]);
f();
let mut b = Box::new("aaa".to_string());
let mut f = || {
let deref!(ref mut s) = b;
s.push_str("aa");
};
f();
assert_eq!(b.len(), 5);
let mut v = vec![1, 2, 3];
let mut f = || {
// this should count as a mutable borrow of `v` as a whole
let [.., ref mut x] = v else { unreachable!() };
*x = 4;
};
f();
assert_eq!(v, [1, 2, 4]);
let mut v = vec![1, 2, 3];
let mut f = || {
// here, `[.., x]` is adjusted by both an overloaded deref and a builtin deref
let [.., x] = &mut v else { unreachable!() };
*x = 4;
};
f();
assert_eq!(v, [1, 2, 4]);
}