@@ -424,7 +424,7 @@ Let's see an example. This Rust code will not compile:
424
424
use std::thread::Thread;
425
425
426
426
fn main () {
427
- let mut numbers = vec! [1is, 2is, 3is ];
427
+ let mut numbers = vec! [1is, 2, 3 ];
428
428
429
429
for i in 0..3 {
430
430
Thread::spawn(move || {
@@ -438,15 +438,15 @@ It gives us this error:
438
438
439
439
` ` ` text
440
440
6:71 error: capture of moved value: ` numbers`
441
- for j in range(0, 3) { numbers[j] += 1 }
442
- ^~~~~~~
441
+ for j in 0..3 { numbers[j] += 1 }
442
+ ^~~~~~~
443
443
7:50 note: ` numbers` moved into closure environment here
444
444
spawn(move || {
445
- for j in range(0, 3) { numbers[j] += 1 }
445
+ for j in 0..3 { numbers[j] += 1 }
446
446
});
447
447
6:79 error: cannot assign to immutable dereference (dereference is implicit, due to indexing)
448
- for j in range(0, 3) { numbers[j] += 1 }
449
- ^~~~~~~~~~~~~~~
448
+ for j in 0..3 { numbers[j] += 1 }
449
+ ^~~~~~~~~~~~~~~
450
450
` ` `
451
451
452
452
It mentions that " numbers moved into closure environment" . Because we
@@ -478,7 +478,7 @@ use std::thread::Thread;
478
478
use std::sync::{Arc,Mutex};
479
479
480
480
fn main () {
481
- let numbers = Arc::new(Mutex::new(vec! [1is, 2is, 3is ]));
481
+ let numbers = Arc::new(Mutex::new(vec! [1is, 2, 3 ]));
482
482
483
483
for i in 0..3 {
484
484
let number = numbers.clone ();
@@ -541,9 +541,9 @@ safety check that makes this an error about moved values:
541
541
use std::thread::Thread;
542
542
543
543
fn main () {
544
- let vec = vec! [1i , 2, 3];
544
+ let vec = vec! [1is , 2, 3];
545
545
546
- for i in range(0u, 3) {
546
+ for i in 0us..3 {
547
547
Thread::spawn(move || {
548
548
println! (" {}" , vec[i]);
549
549
});
@@ -557,9 +557,9 @@ you can remove it. As an example, this is a poor way to iterate through
557
557
a vector:
558
558
559
559
` ` ` {rust}
560
- let vec = vec! [1i , 2, 3];
560
+ let vec = vec! [1 , 2, 3];
561
561
562
- for i in range(0u, vec.len () ) {
562
+ for i in 0.. vec.len () {
563
563
println! (" {}" , vec[i]);
564
564
}
565
565
` ` `
@@ -569,7 +569,7 @@ that we don't try to access an invalid index. However, we can remove this
569
569
while retaining safety. The answer is iterators:
570
570
571
571
```{rust}
572
- let vec = vec![1i , 2, 3];
572
+ let vec = vec![1 , 2, 3];
573
573
574
574
for x in vec.iter() {
575
575
println!("{}", x);
0 commit comments