}
```
-**Note: obviously this is very simple and non-idiomatic C++.
-You wouldn't write it in practice; it is for illustrative purposes.**
+**Note: The above C++ code is deliberately simple and non-idiomatic for the purpose
+of demonstration. It is not representative of production-quality C++ code.**
This function allocates an integer on the stack,
and stores it in a variable, `i`.
```
fn dangling() -> Box<int> {
- let i = box 1234;
+ let i = box 1234i;
return i;
}
}
```
-Now instead of a stack allocated `1234`,
-we have a heap allocated `box 1234`.
+Now instead of a stack allocated `1234i`,
+we have a heap allocated `box 1234i`.
Whereas `&` borrows a pointer to existing memory,
creating an owned box allocates memory on the heap and places a value in it,
giving you the sole pointer to that memory.
```
// Rust
-let i = box 1234;
+let i = box 1234i;
```
```cpp
```
fn main() {
- let numbers = vec![1,2,3];
+ let numbers = vec![1i, 2i, 3i];
let (tx, rx) = channel();
tx.send(numbers);
spawn(proc() {
let numbers = rx.recv();
- println!("{}", *numbers.get(0));
+ println!("{}", numbers[0]);
})
}
```
```ignore
fn main() {
- let numbers = vec![1,2,3];
+ let numbers = vec![1i, 2i, 3i];
let (tx, rx) = channel();
tx.send(numbers);
spawn(proc() {
let numbers = rx.recv();
- println!("{}", numbers.get(0));
+ println!("{}", numbers[0]);
});
// Try to print a number from the original task
- println!("{}", *numbers.get(0));
+ println!("{}", numbers[0]);
}
```
-This will result an error indicating that the value is no longer in scope:
+The compiler will produce an error indicating that the value is no longer in scope:
```text
concurrency.rs:12:20: 12:27 error: use of moved value: 'numbers'
-concurrency.rs:12 println!("{}", numbers.get(0));
+concurrency.rs:12 println!("{}", numbers[0]);
^~~~~~~
```
```
fn main() {
- let numbers = vec![1,2,3];
+ let numbers = vec![1i, 2i, 3i];
- for num in range(0, 3) {
+ for num in range(0u, 3) {
let (tx, rx) = channel();
// Use `clone` to send a *copy* of the array
tx.send(numbers.clone());
spawn(proc() {
let numbers = rx.recv();
- println!("{:d}", *numbers.get(num as uint));
+ println!("{:d}", numbers[num]);
})
}
}
use std::sync::Arc;
fn main() {
- let numbers = vec![1,2,3];
+ let numbers = vec![1i, 2i, 3i];
let numbers = Arc::new(numbers);
- for num in range(0, 3) {
+ for num in range(0u, 3) {
let (tx, rx) = channel();
tx.send(numbers.clone());
spawn(proc() {
let numbers = rx.recv();
- println!("{:d}", *numbers.get(num as uint));
+ println!("{:d}", (*numbers)[num as uint]);
})
}
}
use std::sync::{Arc, Mutex};
fn main() {
- let numbers = vec![1,2,3];
+ let numbers = vec![1i, 2i, 3i];
let numbers_lock = Arc::new(Mutex::new(numbers));
- for num in range(0, 3) {
+ for num in range(0u, 3) {
let (tx, rx) = channel();
tx.send(numbers_lock.clone());
// This is ugly for now, but will be replaced by
// `numbers[num as uint] += 1` in the near future.
- // See: https://github.com/mozilla/rust/issues/6515
+ // See: https://github.com/rust-lang/rust/issues/6515
*numbers.get_mut(num as uint) = *numbers.get_mut(num as uint) + 1;
- println!("{}", *numbers.get(num as uint));
+ println!("{}", (*numbers)[num as uint]);
// When `numbers` goes out of scope the lock is dropped
})