1
1
% The Rust Tasks and Communication Guide
2
2
3
+ ** NOTE** This guide is badly out of date an needs to be rewritten.
4
+
3
5
# Introduction
4
6
5
7
Rust provides safe concurrent abstractions through a number of core library
@@ -22,7 +24,7 @@ from shared mutable state.
22
24
At its simplest, creating a task is a matter of calling the ` spawn ` function
23
25
with a closure argument. ` spawn ` executes the closure in the new task.
24
26
25
- ``` {rust}
27
+ ``` {rust,ignore }
26
28
# use std::task::spawn;
27
29
28
30
// Print something profound in a different task using a named function
@@ -49,7 +51,7 @@ closure is limited to capturing `Send`-able data from its environment
49
51
ensures that ` spawn ` can safely move the entire closure and all its
50
52
associated state into an entirely different task for execution.
51
53
52
- ``` {rust}
54
+ ``` {rust,ignore }
53
55
# use std::task::spawn;
54
56
# fn generate_task_number() -> int { 0 }
55
57
// Generate some state locally
@@ -75,7 +77,7 @@ The simplest way to create a channel is to use the `channel` function to create
75
77
of a channel, and a ** receiver** is the receiving endpoint. Consider the following
76
78
example of calculating two results concurrently:
77
79
78
- ``` {rust}
80
+ ``` {rust,ignore }
79
81
# use std::task::spawn;
80
82
81
83
let (tx, rx): (Sender<int>, Receiver<int>) = channel();
@@ -96,15 +98,15 @@ stream for sending and receiving integers (the left-hand side of the `let`,
96
98
` (tx, rx) ` , is an example of a destructuring let: the pattern separates a tuple
97
99
into its component parts).
98
100
99
- ``` {rust}
101
+ ``` {rust,ignore }
100
102
let (tx, rx): (Sender<int>, Receiver<int>) = channel();
101
103
```
102
104
103
105
The child task will use the sender to send data to the parent task, which will
104
106
wait to receive the data on the receiver. The next statement spawns the child
105
107
task.
106
108
107
- ``` {rust}
109
+ ``` {rust,ignore }
108
110
# use std::task::spawn;
109
111
# fn some_expensive_computation() -> int { 42 }
110
112
# let (tx, rx) = channel();
@@ -123,7 +125,7 @@ computation, then sends the result over the captured channel.
123
125
Finally, the parent continues with some other expensive computation, then waits
124
126
for the child's result to arrive on the receiver:
125
127
126
- ``` {rust}
128
+ ``` {rust,ignore }
127
129
# fn some_other_expensive_computation() {}
128
130
# let (tx, rx) = channel::<int>();
129
131
# tx.send(0);
@@ -154,7 +156,7 @@ spawn(move || {
154
156
155
157
Instead we can clone the ` tx ` , which allows for multiple senders.
156
158
157
- ``` {rust}
159
+ ``` {rust,ignore }
158
160
let (tx, rx) = channel();
159
161
160
162
for init_val in range(0u, 3) {
@@ -179,7 +181,7 @@ Note that the above cloning example is somewhat contrived since you could also
179
181
simply use three ` Sender ` pairs, but it serves to illustrate the point. For
180
182
reference, written with multiple streams, it might look like the example below.
181
183
182
- ``` {rust}
184
+ ``` {rust,ignore }
183
185
# use std::task::spawn;
184
186
185
187
// Create a vector of ports, one for each child task
@@ -203,7 +205,7 @@ getting the result later.
203
205
204
206
The basic example below illustrates this.
205
207
206
- ``` {rust}
208
+ ``` {rust,ignore }
207
209
use std::sync::Future;
208
210
209
211
# fn main() {
@@ -230,7 +232,7 @@ called.
230
232
Here is another example showing how futures allow you to background
231
233
computations. The workload will be distributed on the available cores.
232
234
233
- ``` {rust}
235
+ ``` {rust,ignore }
234
236
# use std::num::Float;
235
237
# use std::sync::Future;
236
238
fn partial_sum(start: uint) -> f64 {
@@ -268,7 +270,7 @@ Here is a small example showing how to use Arcs. We wish to run concurrently
268
270
several computations on a single large vector of floats. Each task needs the
269
271
full vector to perform its duty.
270
272
271
- ``` {rust}
273
+ ``` {rust,ignore }
272
274
use std::num::Float;
273
275
use std::rand;
274
276
use std::sync::Arc;
@@ -295,7 +297,7 @@ The function `pnorm` performs a simple computation on the vector (it computes
295
297
the sum of its items at the power given as argument and takes the inverse power
296
298
of this value). The Arc on the vector is created by the line:
297
299
298
- ``` {rust}
300
+ ``` {rust,ignore }
299
301
# use std::rand;
300
302
# use std::sync::Arc;
301
303
# fn main() {
@@ -309,7 +311,7 @@ the wrapper and not its contents. Within the task's procedure, the captured
309
311
Arc reference can be used as a shared reference to the underlying vector as
310
312
if it were local.
311
313
312
- ``` {rust}
314
+ ``` {rust,ignore }
313
315
# use std::rand;
314
316
# use std::sync::Arc;
315
317
# fn pnorm(nums: &[f64], p: uint) -> f64 { 4.0 }
@@ -346,11 +348,11 @@ and `()`, callers can pattern-match on a result to check whether it's an `Ok`
346
348
result with an ` int ` field (representing a successful result) or an ` Err ` result
347
349
(representing termination with an error).
348
350
349
- ``` {rust}
351
+ ``` {rust,ignore }
350
352
# use std::thread::Thread;
351
353
# fn some_condition() -> bool { false }
352
354
# fn calculate_result() -> int { 0 }
353
- let result: Result<int, Box<std::any::Any + Send>> = Thread::with_join (move || {
355
+ let result: Result<int, Box<std::any::Any + Send>> = Thread::spawn (move || {
354
356
if some_condition() {
355
357
calculate_result()
356
358
} else {
0 commit comments