]> git.lizzy.rs Git - rust.git/blob - tests/ui/borrowck/kindck-implicit-close-over-mut-var.rs
Rollup merge of #106726 - cmorin6:fix-comment-typos, r=Nilstrieb
[rust.git] / tests / ui / borrowck / kindck-implicit-close-over-mut-var.rs
1 // run-pass
2
3 #![allow(unused_must_use)]
4 #![allow(dead_code)]
5 use std::thread;
6
7 fn user(_i: isize) {}
8
9 fn foo() {
10     // Here, i is *copied* into the proc (heap closure).
11     // Requires allocation.  The proc's copy is not mutable.
12     let mut i = 0;
13     let t = thread::spawn(move|| {
14         user(i);
15         println!("spawned {}", i)
16     });
17     i += 1;
18     println!("original {}", i);
19     t.join();
20 }
21
22 fn bar() {
23     // Here, the original i has not been moved, only copied, so is still
24     // mutable outside of the proc.
25     let mut i = 0;
26     while i < 10 {
27         let t = thread::spawn(move|| {
28             user(i);
29         });
30         i += 1;
31         t.join();
32     }
33 }
34
35 fn car() {
36     // Here, i must be shadowed in the proc to be mutable.
37     let mut i = 0;
38     while i < 10 {
39         let t = thread::spawn(move|| {
40             let mut i = i;
41             i += 1;
42             user(i);
43         });
44         i += 1;
45         t.join();
46     }
47 }
48
49 pub fn main() {}