Skip to content

Commit adf30b9

Browse files
committed
Multithreading: Update compiler error messages
They have a new format.
1 parent a57d067 commit adf30b9

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

sections/multithreaded.tex

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,32 @@ \section{Multithreaded Programming}
2727
\end{minted}
2828
\sep
2929
\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+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4344
\end{minted}
4445
\end{frame}
4546

4647
\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}
4756
Let's move the \texttt{data} into the Thread.
4857
\begin{minted}{rust}
4958
let mut data = vec![0];
@@ -61,14 +70,16 @@ \section{Multithreaded Programming}
6170
\end{minted}
6271
\sep
6372
\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
7283
\end{minted}
7384
\end{frame}
7485

@@ -96,19 +107,19 @@ \section{Multithreaded Programming}
96107
\begin{minted}{rust}
97108
let data = Arc::new(vec![0]);
98109

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); });
103112

104113
t1.join().unwrap();
105114
println!("Data: {:?}", data);
106115
\end{minted}
107116
\sep
108117
\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
112123
\end{minted}
113124
\end{frame}
114125

@@ -123,7 +134,7 @@ \section{Multithreaded Programming}
123134
});
124135

125136
t1.join().unwrap();
126-
println!("Data: {:?}", *data.lock().unwrap());
137+
println!("Data: {:?}", data.lock().unwrap());
127138
\end{minted}
128139
\sep
129140
\begin{minted}[fontsize=\footnotesize]{text}

0 commit comments

Comments
 (0)