@@ -198,14 +198,14 @@ Typically, tasks do not share memory but instead communicate amongst each other
198
198
199
199
```
200
200
fn main() {
201
- let numbers = ~ [1,2,3];
201
+ let numbers = vec! [1,2,3];
202
202
203
203
let (tx, rx) = channel();
204
204
tx.send(numbers);
205
205
206
206
spawn(proc() {
207
207
let numbers = rx.recv();
208
- println!("{}", numbers[0] );
208
+ println!("{}", * numbers.get(0) );
209
209
})
210
210
}
211
211
```
@@ -237,26 +237,26 @@ try to modify the previous example to continue using the variable `numbers`:
237
237
238
238
``` ignore
239
239
fn main() {
240
- let numbers = ~ [1,2,3];
240
+ let numbers = vec! [1,2,3];
241
241
242
242
let (tx, rx) = channel();
243
243
tx.send(numbers);
244
244
245
245
spawn(proc() {
246
246
let numbers = rx.recv();
247
- println!("{}", numbers[0] );
247
+ println!("{}", numbers.get(0) );
248
248
});
249
249
250
250
// Try to print a number from the original task
251
- println!("{}", numbers[0] );
251
+ println!("{}", * numbers.get(0) );
252
252
}
253
253
```
254
254
255
255
This will result an error indicating that the value is no longer in scope:
256
256
257
257
``` notrust
258
258
concurrency.rs:12:20: 12:27 error: use of moved value: 'numbers'
259
- concurrency.rs:12 println!("{}", numbers[0] );
259
+ concurrency.rs:12 println!("{}", numbers.get(0) );
260
260
^~~~~~~
261
261
```
262
262
@@ -267,7 +267,7 @@ Let's see an example that uses the `clone` method to create copies of the data:
267
267
268
268
```
269
269
fn main() {
270
- let numbers = ~ [1,2,3];
270
+ let numbers = vec! [1,2,3];
271
271
272
272
for num in range(0, 3) {
273
273
let (tx, rx) = channel();
@@ -276,7 +276,7 @@ fn main() {
276
276
277
277
spawn(proc() {
278
278
let numbers = rx.recv();
279
- println!("{:d}", numbers[ num as uint] );
279
+ println!("{:d}", * numbers.get( num as uint) );
280
280
})
281
281
}
282
282
}
@@ -301,7 +301,7 @@ extern crate sync;
301
301
use sync::Arc;
302
302
303
303
fn main() {
304
- let numbers = ~ [1,2,3];
304
+ let numbers = vec! [1,2,3];
305
305
let numbers = Arc::new(numbers);
306
306
307
307
for num in range(0, 3) {
@@ -310,7 +310,7 @@ fn main() {
310
310
311
311
spawn(proc() {
312
312
let numbers = rx.recv();
313
- println!("{:d}", numbers[ num as uint] );
313
+ println!("{:d}", * numbers.get( num as uint) );
314
314
})
315
315
}
316
316
}
@@ -348,7 +348,7 @@ extern crate sync;
348
348
use sync::{Arc, Mutex};
349
349
350
350
fn main() {
351
- let numbers = ~ [1,2,3];
351
+ let numbers = vec! [1,2,3];
352
352
let numbers_lock = Arc::new(Mutex::new(numbers));
353
353
354
354
for num in range(0, 3) {
@@ -360,9 +360,13 @@ fn main() {
360
360
361
361
// Take the lock, along with exclusive access to the underlying array
362
362
let mut numbers = numbers_lock.lock();
363
- numbers[num as uint] += 1;
364
363
365
- println!("{}", numbers[num as uint]);
364
+ // This is ugly for now, but will be replaced by
365
+ // `numbers[num as uint] += 1` in the near future.
366
+ // See: https://github.com/mozilla/rust/issues/6515
367
+ *numbers.get_mut(num as uint) = *numbers.get_mut(num as uint) + 1;
368
+
369
+ println!("{}", *numbers.get(num as uint));
366
370
367
371
// When `numbers` goes out of scope the lock is dropped
368
372
})
0 commit comments