@@ -162,7 +162,7 @@ The same [ownership system](ownership.html) that helps prevent using pointers
162162incorrectly also helps rule out data races, one of the worst kinds of
163163concurrency bugs.
164164
165- As an example, here is a Rust program that could have a data race in many
165+ As an example, here is a Rust program that would have a data race in many
166166languages. It will not compile:
167167
168168``` ignore
@@ -174,7 +174,7 @@ fn main() {
174174
175175 for i in 0..3 {
176176 thread::spawn(move || {
177- data[i ] += 1 ;
177+ data[0 ] += i ;
178178 });
179179 }
180180
@@ -186,7 +186,7 @@ This gives us an error:
186186
187187``` text
1881888:17 error: capture of moved value: `data`
189- data[i ] += 1 ;
189+ data[0 ] += i ;
190190 ^~~~
191191```
192192
@@ -195,11 +195,6 @@ thread, and the thread takes ownership of the reference, we'd have three owners!
195195` data ` gets moved out of ` main ` in the first call to ` spawn() ` , so subsequent
196196calls in the loop cannot use this variable.
197197
198- Note that this specific example will not cause a data race since different array
199- indices are being accessed. But this can't be determined at compile time, and in
200- a similar situation where ` i ` is a constant or is random, you would have a data
201- race.
202-
203198So, we need some type that lets us have more than one owning reference to a
204199value. Usually, we'd use ` Rc<T> ` for this, which is a reference counted type
205200that provides shared ownership. It has some runtime bookkeeping that keeps track
@@ -223,7 +218,7 @@ fn main() {
223218
224219 // use it in a thread
225220 thread::spawn(move || {
226- data_ref[i ] += 1 ;
221+ data_ref[0 ] += i ;
227222 });
228223 }
229224
@@ -266,7 +261,7 @@ fn main() {
266261 for i in 0..3 {
267262 let data = data.clone();
268263 thread::spawn(move || {
269- data[i ] += 1 ;
264+ data[0 ] += i ;
270265 });
271266 }
272267
@@ -281,7 +276,7 @@ And... still gives us an error.
281276
282277``` text
283278<anon>:11:24 error: cannot borrow immutable borrowed content as mutable
284- <anon>:11 data[i ] += 1 ;
279+ <anon>:11 data[0 ] += i ;
285280 ^~~~
286281```
287282
@@ -317,7 +312,7 @@ fn main() {
317312 let data = data . clone ();
318313 thread :: spawn (move || {
319314 let mut data = data . lock (). unwrap ();
320- data [i ] += 1 ;
315+ data [0 ] += i ;
321316 });
322317 }
323318
@@ -360,7 +355,7 @@ Let's examine the body of the thread more closely:
360355# let data = data . clone ();
361356thread :: spawn (move || {
362357 let mut data = data . lock (). unwrap ();
363- data [i ] += 1 ;
358+ data [0 ] += i ;
364359});
365360# }
366361# thread :: sleep (Duration :: from_millis (50 ));
0 commit comments