Skip to content
4 changes: 2 additions & 2 deletions doc/guide-conditions.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,15 @@ use std::task;
fn main() {

// Isolate failure within a subtask.
let result = do task::try {
let result = task::try(proc() {

// The protected logic.
let pairs = read_int_pairs();
for &(a,b) in pairs.iter() {
println!("{:4.4d}, {:4.4d}", a, b);
}

};
});
if result.is_err() {
println!("parsing failed");
}
Expand Down
8 changes: 4 additions & 4 deletions doc/guide-pointers.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ struct Point {

fn main() {
let a = Point { x: 10, y: 20 };
do spawn {
spawn(proc() {
println!("{}", a.x);
}
});
}
~~~

Expand All @@ -238,9 +238,9 @@ struct Point {

fn main() {
let a = ~Point { x: 10, y: 20 };
do spawn {
spawn(proc() {
println!("{}", a.x);
}
});
}
~~~

Expand Down
4 changes: 2 additions & 2 deletions doc/guide-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,9 @@ extern mod green;

#[start]
fn start(argc: int, argv: **u8) -> int {
do green::start(argc, argv) {
green::start(argc, argv, proc() {
main();
}
})
}

fn main() {}
Expand Down
47 changes: 21 additions & 26 deletions doc/guide-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ spawn(print_message);

// Print something more profound in a different task using a lambda expression
spawn(proc() println!("I am also running in a different task!") );

// The canonical way to spawn is using `do` notation
do spawn {
println!("I too am running in a different task!");
}
~~~~

In Rust, there is nothing special about creating tasks: a task is not a
Expand All @@ -103,10 +98,10 @@ an environment that it carries across tasks.
// Generate some state locally
let child_task_number = generate_task_number();

do spawn {
spawn(proc() {
// Capture it in the remote task
println!("I am child number {}", child_task_number);
}
});
~~~

## Communication
Expand All @@ -132,10 +127,10 @@ concurrently:

let (port, chan): (Port<int>, Chan<int>) = Chan::new();

do spawn || {
spawn(proc() {
let result = some_expensive_computation();
chan.send(result);
}
});

some_other_expensive_computation();
let result = port.recv();
Expand All @@ -160,10 +155,10 @@ spawns the child task.
# use std::task::spawn;
# fn some_expensive_computation() -> int { 42 }
# let (port, chan) = Chan::new();
do spawn || {
spawn(proc() {
let result = some_expensive_computation();
chan.send(result);
}
});
~~~~

Notice that the creation of the task closure transfers `chan` to the child
Expand Down Expand Up @@ -195,15 +190,15 @@ of tasks? The following program is ill-typed:
# fn some_expensive_computation() -> int { 42 }
let (port, chan) = Chan::new();

do spawn {
spawn(proc() {
chan.send(some_expensive_computation());
}
});

// ERROR! The previous spawn statement already owns the channel,
// so the compiler will not allow it to be captured again
do spawn {
spawn(proc() {
chan.send(some_expensive_computation());
}
});
~~~

Instead we can use a `SharedChan`, a type that allows a single
Expand All @@ -217,9 +212,9 @@ let (port, chan) = SharedChan::new();
for init_val in range(0u, 3) {
// Create a new channel handle to distribute to the child task
let child_chan = chan.clone();
do spawn {
spawn(proc() {
child_chan.send(some_expensive_computation(init_val));
}
});
}

let result = port.recv() + port.recv() + port.recv();
Expand Down Expand Up @@ -247,9 +242,9 @@ might look like the example below.
// Create a vector of ports, one for each child task
let ports = vec::from_fn(3, |init_val| {
let (port, chan) = Chan::new();
do spawn {
spawn(proc() {
chan.send(some_expensive_computation(init_val));
}
});
port
});

Expand Down Expand Up @@ -296,7 +291,7 @@ fn partial_sum(start: uint) -> f64 {
}

fn main() {
let mut futures = vec::from_fn(1000, |ind| do extra::future::Future::spawn { partial_sum(ind) });
let mut futures = vec::from_fn(1000, |ind| extra::future::Future::spawn( proc() { partial_sum(ind) }));

let mut final_res = 0f64;
for ft in futures.mut_iter() {
Expand Down Expand Up @@ -339,11 +334,11 @@ fn main() {
let (port, chan) = Chan::new();
chan.send(numbers_arc.clone());

do spawn {
spawn(proc() {
let local_arc : Arc<~[f64]> = port.recv();
let task_numbers = local_arc.get();
println!("{}-norm = {}", num, pnorm(task_numbers, num));
}
});
}
}
~~~
Expand Down Expand Up @@ -417,13 +412,13 @@ termination with an error).
# use std::task;
# fn some_condition() -> bool { false }
# fn calculate_result() -> int { 0 }
let result: Result<int, ()> = do task::try {
let result: Result<int, ()> = task::try(proc() {
if some_condition() {
calculate_result()
} else {
fail!("oops!");
}
};
});
assert!(result.is_err());
~~~

Expand Down Expand Up @@ -502,9 +497,9 @@ Here is the code for the parent task:

let (from_child, to_child) = DuplexStream::new();

do spawn {
spawn(proc() {
stringifier(&to_child);
};
});

from_child.send(22);
assert!(from_child.recv() == ~"22");
Expand Down
46 changes: 0 additions & 46 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -2751,52 +2751,6 @@ but must enclose it.

A `loop` expression is only permitted in the body of a loop.

### Do expressions

~~~~ {.ebnf .gram}
do_expr : "do" expr [ '|' ident_list '|' ] ? '{' block '}' ;
~~~~

A _do expression_ provides a more-familiar block syntax
for invoking a function and passing it a newly-created a procedure.

The optional `ident_list` and `block` provided in a `do` expression are parsed
as though they constitute a procedure expression;
if the `ident_list` is missing, an empty `ident_list` is implied.

The procedure expression is then provided as a _trailing argument_
to the outermost [call](#call-expressions) or
[method call](#method-call-expressions) expression
in the `expr` following `do`.
If the `expr` is a [path expression](#path-expressions), it is parsed as though it is a call expression.
If the `expr` is a [field expression](#field-expressions), it is parsed as though it is a method call expression.

In this example, both calls to `f` are equivalent:

~~~~
# fn f(f: proc(int)) { }
# fn g(i: int) { }

f(proc(j) { g(j) });

do f |j| {
g(j);
}
~~~~

In this example, both calls to the (binary) function `k` are equivalent:

~~~~
# fn k(x:int, f: proc(int)) { }
# fn l(i: int) { }

k(3, proc(j) { l(j) });

do k(3) |j| {
l(j);
}
~~~~

### For expressions

~~~~ {.ebnf .gram}
Expand Down
46 changes: 9 additions & 37 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -1796,48 +1796,20 @@ call_it(proc(n) {
});
~~~~

This is such a useful pattern that Rust has a special form of function
call for these functions.

~~~~
# fn call_it(op: proc(v: int)) { }
do call_it() |n| {
println!("{}", n);
}
~~~~

The call is prefixed with the keyword `do` and, instead of writing the
final procedure inside the argument list, it appears outside of the
parentheses, where it looks more like a typical block of
code.

`do` is a convenient way to create tasks with the `task::spawn`
function. `spawn` has the signature `spawn(fn: proc())`. In other
words, it is a function that takes an owned closure that takes no
arguments.

~~~~
use std::task::spawn;

do spawn() || {
debug!("I'm a task, whatever");
}
~~~~

Look at all those bars and parentheses -- that's two empty argument
lists back to back. Since that is so unsightly, empty argument lists
may be omitted from `do` expressions.
A practical example of this pattern is found when using the `spawn` function,
which starts a new task.

~~~~
use std::task::spawn;

do spawn {
debug!("Kablam!");
}
spawn(proc() {
debug!("I'm a new task")
});
~~~~

If you want to see the output of `debug!` statements, you will need to turn on `debug!` logging.
To enable `debug!` logging, set the RUST_LOG environment variable to the name of your crate, which, for a file named `foo.rs`, will be `foo` (e.g., with bash, `export RUST_LOG=foo`).
If you want to see the output of `debug!` statements, you will need to turn on
`debug!` logging. To enable `debug!` logging, set the RUST_LOG environment
variable to the name of your crate, which, for a file named `foo.rs`, will be
`foo` (e.g., with bash, `export RUST_LOG=foo`).

# Methods

Expand Down
4 changes: 2 additions & 2 deletions src/compiletest/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,10 @@ fn run_debuginfo_test(config: &config, props: &TestProps, testfile: &Path) {
loop {
//waiting 1 second for gdbserver start
timer::sleep(1000);
let result = do task::try {
let result = task::try(proc() {
tcp::TcpStream::connect(
SocketAddr { ip: Ipv4Addr(127, 0, 0, 1), port: 5039 });
};
});
if result.is_err() {
continue;
}
Expand Down
Loading