blob: 9f0c4d96aa5d923429db5dbce51a837f33d702a3 (
plain)
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
|
// edition:2021
// run-pass
// Test that we can mutate a place through a mut-borrow
// that is captured by the closure
// Check that we can mutate when one deref is required
fn mut_ref_1() {
let mut x = String::new();
let rx = &mut x;
let mut c = || {
*rx = String::new();
};
c();
}
// Similar example as mut_ref_1, we don't deref the imm-borrow here,
// and so we are allowed to mutate.
fn mut_ref_2() {
let x = String::new();
let y = String::new();
let mut ref_x = &x;
let m_ref_x = &mut ref_x;
let mut c = || {
*m_ref_x = &y;
};
c();
}
// Check that we can mutate when multiple derefs of mut-borrows are required to reach
// the target place.
// It works because all derefs are mutable, if either of them was an immutable
// borrow, then we would not be able to deref.
fn mut_mut_ref() {
let mut x = String::new();
let mut mref_x = &mut x;
let m_mref_x = &mut mref_x;
let mut c = || {
**m_mref_x = String::new();
};
c();
}
fn main() {
mut_ref_1();
mut_ref_2();
mut_mut_ref();
}
|