use std::thread::Thread;
fn main() {
- let mut numbers = vec![1i, 2i, 3i];
+ let mut numbers = vec![1is, 2, 3];
- for i in range(0u, 3u) {
+ for i in 0..3 {
Thread::spawn(move || {
- for j in range(0, 3) { numbers[j] += 1 }
+ for j in 0..3 { numbers[j] += 1 }
});
}
}
```text
6:71 error: capture of moved value: `numbers`
- for j in range(0, 3) { numbers[j] += 1 }
- ^~~~~~~
+ for j in 0..3 { numbers[j] += 1 }
+ ^~~~~~~
7:50 note: `numbers` moved into closure environment here
spawn(move || {
- for j in range(0, 3) { numbers[j] += 1 }
+ for j in 0..3 { numbers[j] += 1 }
});
6:79 error: cannot assign to immutable dereference (dereference is implicit, due to indexing)
- for j in range(0, 3) { numbers[j] += 1 }
- ^~~~~~~~~~~~~~~
+ for j in 0..3 { numbers[j] += 1 }
+ ^~~~~~~~~~~~~~~
```
It mentions that "numbers moved into closure environment". Because we
use std::sync::{Arc,Mutex};
fn main() {
- let numbers = Arc::new(Mutex::new(vec![1i, 2i, 3i]));
+ let numbers = Arc::new(Mutex::new(vec![1is, 2, 3]));
- for i in range(0u, 3u) {
+ for i in 0us..3 {
let number = numbers.clone();
Thread::spawn(move || {
let mut array = number.lock().unwrap();
-
- (*array)[i] += 1;
-
- println!("numbers[{}] is {}", i, (*array)[i]);
+ array[i] += 1;
+ println!("numbers[{}] is {}", i, array[i]);
});
}
}
use std::thread::Thread;
fn main() {
- let vec = vec![1i, 2, 3];
+ let vec = vec![1is, 2, 3];
- for i in range(0u, 3) {
+ for i in 0us..3 {
Thread::spawn(move || {
println!("{}", vec[i]);
- }).detach();
+ });
}
}
```
a vector:
```{rust}
-let vec = vec![1i, 2, 3];
+let vec = vec![1, 2, 3];
-for i in range(0u, vec.len()) {
+for i in 0..vec.len() {
println!("{}", vec[i]);
}
```
while retaining safety. The answer is iterators:
```{rust}
-let vec = vec![1i, 2, 3];
+let vec = vec![1, 2, 3];
for x in vec.iter() {
println!("{}", x);