@@ -27,23 +27,32 @@ \section{Multithreaded Programming}
27
27
\end {minted }
28
28
\sep
29
29
\begin {minted }[fontsize=\footnotesize ]{text}
30
- error: closure may outlive the current function, but it borrows `data`,
31
- which is owned by the current function [E0373]
32
- let t1 = thread::spawn(|| {
33
- data.push(19);
34
- });
35
- note: `data` is borrowed here
36
- data.push(19);
37
- ^~~~
38
- help: to force the closure to take ownership of `data` (and any other
39
- referenced variables), use the `move` keyword, as shown:
40
- let t1 = thread::spawn(move || {
41
- data.push(19);
42
- });
30
+ error[E0373]: closure may outlive the current function, but it borrows `data`,
31
+ which is owned by the current function
32
+ --> src/main.rs:5:28
33
+ |
34
+ 5 | let t1 = thread::spawn(|| { data.push(19); });
35
+ | ^^ ---- `data` is borrowed here
36
+ | |
37
+ | may outlive borrowed value `data`
38
+ |
39
+ note: function requires argument type to outlive `'static`
40
+ --> src/main.rs:5:14
41
+ |
42
+ 5 | let t1 = thread::spawn(|| { data.push(19); });
43
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
43
44
\end {minted }
44
45
\end {frame }
45
46
46
47
\begin {frame }[fragile]{Shared Data (2) -- Move data}
48
+ The compiler also gives us a helpful hint:
49
+ \begin {minted }[fontsize=\footnotesize ]{text}
50
+ help: to force the closure to take ownership of `data` (and any
51
+ other referenced variables), use the `move` keyword
52
+ |
53
+ 5 | let t1 = thread::spawn(move || { data.push(19); });
54
+ | ^^^^^^^
55
+ \end {minted }
47
56
Let's move the \texttt {data } into the Thread.
48
57
\begin {minted }{rust}
49
58
let mut data = vec![0];
@@ -61,14 +70,16 @@ \section{Multithreaded Programming}
61
70
\end {minted }
62
71
\sep
63
72
\begin {minted }[fontsize=\footnotesize ]{text}
64
- error: use of moved value: `data` [E0382]
65
- println!("Data: {:?}", data);
66
- ^~~~
67
- note: `data` moved into closure environment here because it has
68
- type `collections::vec::Vec<i32>`, which is non-copyable
69
- let t1 = thread::spawn(move || { data.push(19); });
70
- ^~~~~~~~~~~~~~~~~~~~~~~~~~
71
- help: perhaps you meant to use `clone()`?
73
+ error[E0382]: borrow of moved value: `data`
74
+ 4 | let mut data = vec![0];
75
+ | -------- move occurs because `data` has type `std::vec::Vec<i32>`,
76
+ | which does not implement the `Copy` trait
77
+ 5 | let t1 = thread::spawn(move || { data.push(19); });
78
+ | ------- ---- variable moved due to use
79
+ | | in closure
80
+ | value moved into closure here
81
+ 7 | println!("Data: {:?}", data);
82
+ | ^^^^ value borrowed here after move
72
83
\end {minted }
73
84
\end {frame }
74
85
@@ -96,19 +107,19 @@ \section{Multithreaded Programming}
96
107
\begin {minted }{rust}
97
108
let data = Arc::new(vec![0]);
98
109
99
- let mut data2 = data.clone();
100
- let t1 = thread::spawn(move || {
101
- data2.push(1);
102
- });
110
+ let data2 = data.clone();
111
+ let t1 = thread::spawn(move || { data2.push(1); });
103
112
104
113
t1.join().unwrap();
105
114
println!("Data: {:?}", data);
106
115
\end {minted }
107
116
\sep
108
117
\begin {minted }[fontsize=\footnotesize ]{text}
109
- error: cannot borrow immutable borrowed content as mutable
110
- data2.push(1);
111
- ^~~~~
118
+ error[E0596]: cannot borrow data in a `&` reference as mutable
119
+ --> src/main.rs:8:38
120
+ |
121
+ 8 | let t1 = thread::spawn(move || { data2.push(1); });
122
+ | ^^^^^ cannot borrow as mutable
112
123
\end {minted }
113
124
\end {frame }
114
125
@@ -123,7 +134,7 @@ \section{Multithreaded Programming}
123
134
});
124
135
125
136
t1.join().unwrap();
126
- println!("Data: {:?}", * data.lock().unwrap());
137
+ println!("Data: {:?}", data.lock().unwrap());
127
138
\end {minted }
128
139
\sep
129
140
\begin {minted }[fontsize=\footnotesize ]{text}
0 commit comments