]> git.proxmox.com Git - rustc.git/blob - tests/ui/closures/2229_closure_analysis/run_pass/mut_ref.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / tests / ui / closures / 2229_closure_analysis / run_pass / mut_ref.rs
1 // edition:2021
2 // run-pass
3
4 // Test that we can mutate a place through a mut-borrow
5 // that is captured by the closure
6
7 // Check that we can mutate when one deref is required
8 fn mut_ref_1() {
9 let mut x = String::new();
10 let rx = &mut x;
11
12 let mut c = || {
13 *rx = String::new();
14 };
15
16 c();
17 }
18
19 // Similar example as mut_ref_1, we don't deref the imm-borrow here,
20 // and so we are allowed to mutate.
21 fn mut_ref_2() {
22 let x = String::new();
23 let y = String::new();
24 let mut ref_x = &x;
25 let m_ref_x = &mut ref_x;
26
27 let mut c = || {
28 *m_ref_x = &y;
29 };
30
31 c();
32 }
33
34 // Check that we can mutate when multiple derefs of mut-borrows are required to reach
35 // the target place.
36 // It works because all derefs are mutable, if either of them was an immutable
37 // borrow, then we would not be able to deref.
38 fn mut_mut_ref() {
39 let mut x = String::new();
40 let mut mref_x = &mut x;
41 let m_mref_x = &mut mref_x;
42
43 let mut c = || {
44 **m_mref_x = String::new();
45 };
46
47 c();
48 }
49
50 fn main() {
51 mut_ref_1();
52 mut_ref_2();
53 mut_mut_ref();
54 }