@@ -389,11 +389,11 @@ safe concurrent programs.
389
389
Here' s an example of a concurrent Rust program:
390
390
391
391
` ` ` {rust}
392
- use std::thread::Thread ;
392
+ use std::thread;
393
393
394
394
fn main () {
395
395
let guards: Vec< _> = (0..10).map(| _| {
396
- Thread ::scoped(|| {
396
+ thread ::scoped(|| {
397
397
println! (" Hello, world!" );
398
398
})
399
399
}).collect();
@@ -421,13 +421,13 @@ problem.
421
421
Let' s see an example. This Rust code will not compile:
422
422
423
423
` ` ` {rust,ignore}
424
- use std::thread::Thread ;
424
+ use std::thread;
425
425
426
426
fn main () {
427
427
let mut numbers = vec! [1, 2, 3];
428
428
429
429
let guards: Vec< _> = (0..3).map(| i| {
430
- Thread ::scoped(move || {
430
+ thread ::scoped(move || {
431
431
numbers[i] += 1;
432
432
println! (" numbers[{}] is {}" , i, numbers[i]);
433
433
});
@@ -439,7 +439,7 @@ It gives us this error:
439
439
440
440
` ` ` text
441
441
7:25: 10:6 error: cannot move out of captured outer variable in an ` FnMut` closure
442
- 7 Thread ::scoped(move || {
442
+ 7 thread ::scoped(move || {
443
443
8 numbers[i] += 1;
444
444
9 println! (" numbers[{}] is {}" , i, numbers[i]);
445
445
10 });
@@ -471,15 +471,15 @@ mutation doesn't cause a data race.
471
471
Here' s what using an Arc with a Mutex looks like:
472
472
473
473
` ` ` {rust}
474
- use std::thread::Thread ;
474
+ use std::thread;
475
475
use std::sync::{Arc,Mutex};
476
476
477
477
fn main () {
478
478
let numbers = Arc::new(Mutex::new(vec! [1, 2, 3]));
479
479
480
480
let guards: Vec< _> = (0..3).map(| i| {
481
481
let number = numbers.clone ();
482
- Thread ::scoped(move || {
482
+ thread ::scoped(move || {
483
483
let mut array = number.lock().unwrap ();
484
484
array[i] += 1;
485
485
println! (" numbers[{}] is {}" , i, array[i]);
@@ -535,13 +535,13 @@ As an example, Rust's ownership system is _entirely_ at compile time. The
535
535
safety check that makes this an error about moved values:
536
536
537
537
` ` ` {rust,ignore}
538
- use std::thread::Thread ;
538
+ use std::thread;
539
539
540
540
fn main () {
541
541
let numbers = vec! [1, 2, 3];
542
542
543
543
let guards: Vec< _> = (0..3).map(| i| {
544
- Thread ::scoped(move || {
544
+ thread ::scoped(move || {
545
545
println! (" {}" , numbers[i]);
546
546
});
547
547
}).collect();
0 commit comments