]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/kindck-implicit-close-over-mut-var.rs
Rollup merge of #53110 - Xanewok:save-analysis-remap-path, r=nrc
[rust.git] / src / test / run-pass / kindck-implicit-close-over-mut-var.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::thread;
12
13 fn user(_i: isize) {}
14
15 fn foo() {
16     // Here, i is *copied* into the proc (heap closure).
17     // Requires allocation.  The proc's copy is not mutable.
18     let mut i = 0;
19     let t = thread::spawn(move|| {
20         user(i);
21         println!("spawned {}", i)
22     });
23     i += 1;
24     println!("original {}", i);
25     t.join();
26 }
27
28 fn bar() {
29     // Here, the original i has not been moved, only copied, so is still
30     // mutable outside of the proc.
31     let mut i = 0;
32     while i < 10 {
33         let t = thread::spawn(move|| {
34             user(i);
35         });
36         i += 1;
37         t.join();
38     }
39 }
40
41 fn car() {
42     // Here, i must be shadowed in the proc to be mutable.
43     let mut i = 0;
44     while i < 10 {
45         let t = thread::spawn(move|| {
46             let mut i = i;
47             i += 1;
48             user(i);
49         });
50         i += 1;
51         t.join();
52     }
53 }
54
55 pub fn main() {}