Skip to content

Rolling up PRs in the queue #20320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 124 commits into from
Dec 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
124 commits
Select commit Hold shift + click to select a range
aa37b6d
Update json.rs
brianloveswords Dec 23, 2014
556e3da
core: Removed a shadowed, unused definition of `debug_assert!`.
lifthrasiir Dec 24, 2014
ffd0f5a
Fix a typo
nagisa Dec 24, 2014
0eafc32
doc: remove repeated info
tshepang Dec 24, 2014
6ca45c3
doc: surround with symbols, like it should
tshepang Dec 24, 2014
1114685
hashmap: Fix the example using derived Hash + Eq
Dec 25, 2014
6256973
Rename remaining hashmap and hashtable iterators to match naming
csouth3 Dec 25, 2014
e8b151b
Fix backtrace demangling
sfackler Dec 25, 2014
f1e37f9
trans: Remove is_lang_item from base::invoke
reem Dec 2, 2014
7fcd2c2
Fix typo in std::thread comments
aochagavia Dec 25, 2014
72c8f37
Prepared most `StrExt` pattern using methods for stabilization
Kimundi Dec 18, 2014
e656081
Accept `?Sized` as well as `Sized?`
nrc Dec 24, 2014
4688aad
Update tests to use `?Sized`
nrc Dec 24, 2014
9092eba
Removed the sharding bit from mk/tests.mk
Dec 24, 2014
eb4b202
Map EEXIST to PathAlreadyExists error, closes #20226
fhahn Dec 25, 2014
c4640a2
Changes to RustDoc
nrc Dec 24, 2014
d25357c
Add a test case for issue 18906
jroesch Dec 24, 2014
2fefb22
Fixed minor typo in docs for `Result`'s `err` method
YawarRaza7349 Dec 26, 2014
9449161
Do not resolve labels across function boundary
sanxiyn Dec 26, 2014
8387d1e
add new-style Unicode escapes
bombless Dec 26, 2014
3358e64
Derive Clone, PartialEq, and Eq for std::io::{FileAccess, FileMode}
ipetkov Dec 26, 2014
b8ffad5
s/task/thread/g
steveklabnik Dec 26, 2014
0204c1f
Remove some `ignore`s from the guide.
huonw Dec 27, 2014
fbda51e
Address review comments
sanxiyn Dec 27, 2014
35a6f62
Fix spans for `use` view statements and their treatment in save-analysis
nrc Dec 27, 2014
e55b793
save-analysis: give the correct fully qualified name for fields in st…
nrc Dec 27, 2014
25a77fb
save-analysis: fix spans for fields in struct patterns
nrc Dec 27, 2014
ad39c0a
Update docstring for bitflags macro to cover all generated methods
nicholasbishop Dec 28, 2014
4c4eabf
save-analysis: fix spans for paths to struct variants
nrc Dec 28, 2014
875af5b
`man rustc` and `rustc --help` say options go first
mdinger Dec 28, 2014
4477c7c
Indent where clause in rustdoc
dirk Dec 28, 2014
c095bf5
`once` is not keyword now
bombless Dec 28, 2014
b94bb87
Fixes invalid LLVM data layout for aggregate data types
vhbit Dec 28, 2014
1e89bbc
Rename TaskRng to ThreadRng
nagisa Dec 28, 2014
3725067
Regression test for #3902
tamird Dec 22, 2014
5e9a2ab
Update test for #5543
tamird Dec 23, 2014
dd8fdff
Regression test for #8874
tamird Dec 23, 2014
ee6b97d
Regression test for simple case of #9197
tamird Dec 23, 2014
0579d58
Regression test for #13655
tamird Dec 23, 2014
885d7de
Regression test for #13665
tamird Dec 23, 2014
7a758d1
Regression test for #13808
tamird Dec 23, 2014
9cd7864
Regression tests for #13853
tamird Dec 23, 2014
1fd491c
Regression test for #14386
tamird Dec 23, 2014
b745a4f
Regression test for #14227
tamird Dec 23, 2014
d1438f5
Regression test for #15034
tamird Dec 23, 2014
57a3ef3
Use CSS whitespace rather than padding to indent
dirk Dec 28, 2014
4804841
mk: Stop generating docs for deprecated crates
alexcrichton Dec 21, 2014
01cdf00
Regression test for #16538
tamird Dec 23, 2014
2d10021
Regression test for #17728
tamird Dec 23, 2014
252423f
Regression test for #17740
tamird Dec 23, 2014
d9e0bbc
Update curl flag and add additional two-step installation instructions.
inthecloud247 Dec 28, 2014
ae5a5f5
term: Deprecate the in-tree version
alexcrichton Dec 28, 2014
ccd185e
update curl output flag.
inthecloud247 Dec 28, 2014
a454997
src/libstd/thread_local/scoped.rs: fixes scoped_thread_local! macro
arturoc Dec 27, 2014
02e03e9
Fix deprecation warnings on libserialize tests
dgiagio Dec 28, 2014
f33ec5e
Fix deprecation warnings on libregex tests
dgiagio Dec 28, 2014
8c881cd
Fix incorrect link in Makefile.in documentation
WebDrake Dec 28, 2014
ba17759
Make lifetime elision help more useful on type signatures.
crhino Dec 24, 2014
f53314c
Remove the glob/shadowing exception bug
nrc Dec 28, 2014
9c1567e
Fallout from glob shadowing
nrc Dec 28, 2014
9a58808
Little bit of refactoring in resolve
nrc Dec 28, 2014
35dd33c
Fix glob shadowing bug with re-exports
nrc Dec 29, 2014
ac09535
Fallout from globs/re-export/shadowing change
nrc Dec 29, 2014
9dce83c
Tests
nrc Dec 29, 2014
c1f3aca
Marked find and rfind as stable
Kimundi Dec 29, 2014
76e5ed6
std: Return Result from RWLock/Mutex methods
alexcrichton Dec 9, 2014
19f73b4
auto merge of #20058 : Kimundi/rust/str_pattern_pre, r=alexcrichton
bors Dec 29, 2014
2881953
Fix output directory for generated antlr code
fhahn Dec 26, 2014
adda899
Update grammer/verify.rs to work with recent master
fhahn Dec 26, 2014
7112390
auto merge of #20101 : alexcrichton/rust/issue-20096, r=aturon
bors Dec 29, 2014
88d4e02
Implement Send for Cell and RefCell
sfackler Dec 29, 2014
808945c
Handle range in model lexer correctly #15877
fhahn Dec 29, 2014
b26daf3
std: Second pass stabilization for `string`
alexcrichton Dec 28, 2014
35e63e3
std: Stabilization pass for mutex/rwlock/condvar
alexcrichton Dec 29, 2014
54452cd
std: Second pass stabilization for `ptr`
alexcrichton Dec 19, 2014
ed8f503
Add hypothetical support for ranges with only an upper bound
nrc Dec 18, 2014
4e2afb0
Remove ExprSlice by hacking the compiler
nrc Dec 18, 2014
3bf4056
Fallout from mut slices
nrc Dec 18, 2014
113f8aa
Rebasing and reviewer changes
nrc Dec 23, 2014
94d82c1
rollup merge of #19457: reem/remove-is-lang-item
alexcrichton Dec 30, 2014
cc20d60
rollup merge of #19661: alexcrichton/mutex-result
alexcrichton Dec 30, 2014
52315a9
rollup merge of #20042: alexcrichton/second-pass-ptr
alexcrichton Dec 30, 2014
dbc8440
rollup merge of #20160: nick29581/ranges2
alexcrichton Dec 30, 2014
62c9a48
rollup merge of #20165: tamird/needstest-tests
alexcrichton Dec 30, 2014
7b3be9b
rollup merge of #20182: brianloveswords/patch-2
alexcrichton Dec 30, 2014
28f424f
rollup merge of #20191: lifthrasiir/double-debug_assert
alexcrichton Dec 30, 2014
2a85477
rollup merge of #20194: nick29581/dst-syntax
alexcrichton Dec 30, 2014
0625658
rollup merge of #20195: nagisa/unused-typo
alexcrichton Dec 30, 2014
12ac91e
rollup merge of #20205: tshepang/patch-4
alexcrichton Dec 30, 2014
88ea54a
rollup merge of #20207: jroesch/issue-18906-testcase
alexcrichton Dec 30, 2014
9ac9d7a
rollup merge of #20210: tshepang/patch-5
alexcrichton Dec 30, 2014
9f6eb29
rollup merge of #20214: bluss/fix-hashmap-example
alexcrichton Dec 30, 2014
748440c
rollup merge of #20215: csouth3/hashmap-rename
alexcrichton Dec 30, 2014
0fbd1e2
rollup merge of #20216: sfackler/fix-mangling
alexcrichton Dec 30, 2014
0d95b41
rollup merge of #20223: aochagavia/typo
alexcrichton Dec 30, 2014
bcd3b16
rollup merge of #20230: bheesham/noshard
alexcrichton Dec 30, 2014
3801c26
rollup merge of #20231: fhahn/issue-20226-eexist
alexcrichton Dec 30, 2014
21f661a
rollup merge of #20239: YawarRaza7349/patch-1
alexcrichton Dec 30, 2014
4dacf27
rollup merge of #20242: sanxiyn/break-from-fn
alexcrichton Dec 30, 2014
b31926d
rollup merge of #20243: bombless/patch-1
alexcrichton Dec 30, 2014
6fabf42
rollup merge of #20245: fhahn/make-lexer-tests-runable-again
alexcrichton Dec 30, 2014
4717f07
rollup merge of #20248: steveklabnik/gh20038
alexcrichton Dec 30, 2014
1c61e74
rollup merge of #20250: ipetkov/deriving
alexcrichton Dec 30, 2014
2fea594
rollup merge of #20252: huonw/doc-no-ignore
alexcrichton Dec 30, 2014
6b473b2
rollup merge of #20262: arturoc/fix-scoped_thread_local
alexcrichton Dec 30, 2014
3385ff7
rollup merge of #20263: crhino/lifetime-elision-help
alexcrichton Dec 30, 2014
9cbbfee
rollup merge of #20264: nagisa/threadrng
alexcrichton Dec 30, 2014
b1dec37
rollup merge of #20265: nicholasbishop/bishop_add_missing_bitflags_me…
alexcrichton Dec 30, 2014
066be2a
rollup merge of #20266: nick29581/dxr-use
alexcrichton Dec 30, 2014
1d373ae
rollup merge of #20268: mdinger/pretty
alexcrichton Dec 30, 2014
1e9c7c5
rollup merge of #20269: dirk/indent-where
alexcrichton Dec 30, 2014
7b5b0b1
rollup merge of #20270: bombless/patch-2
alexcrichton Dec 30, 2014
f8fc141
rollup merge of #20271: vhbit/datalayout-fix
alexcrichton Dec 30, 2014
c9531b4
rollup merge of #20275: inthecloud247/patch-1
alexcrichton Dec 30, 2014
de0749f
rollup merge of #20276: alexcrichton/deprecate-term
alexcrichton Dec 30, 2014
e787fb9
rollup merge of #20279: dgiagio/libserialize_deprecated_fix1
alexcrichton Dec 30, 2014
259c4be
rollup merge of #20281: dgiagio/libregex_deprecated_fix1
alexcrichton Dec 30, 2014
d7a09cb
rollup merge of #20282: WebDrake/makefile-in
alexcrichton Dec 30, 2014
806cb35
rollup merge of #20289: nick29581/shadowing
alexcrichton Dec 30, 2014
79db01a
rollup merge of #20306: alexcrichton/second-pass-string
alexcrichton Dec 30, 2014
731fcfc
rollup merge of #20309: sfackler/refcell-send
alexcrichton Dec 30, 2014
021c2f3
rollup merge of #20310: fhahn/issue-15877-model-lexer-range
alexcrichton Dec 30, 2014
cb7599b
rollup merge of #20317: brson/rust-installer-v2
alexcrichton Dec 30, 2014
470ae10
Test fixes and rebase conflicts
alexcrichton Dec 30, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
# This is hardly all there is to know of The Rust Build System's
# mysteries. The tale continues on the wiki[1][2].
#
# [1]: https://github.com/rust-lang/rust/wiki/Note-build-system
# [1]: https://github.com/rust-lang/rust/wiki/Note-getting-started-developing-Rust
# [2]: https://github.com/rust-lang/rust/wiki/Note-testsuite
#
# If you really feel like getting your hands dirty, then:
Expand Down
7 changes: 6 additions & 1 deletion mk/crates.mk
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ DOC_CRATES := $(filter-out rustc, \
$(filter-out rustc_borrowck, \
$(filter-out rustc_resolve, \
$(filter-out rustc_driver, \
$(filter-out syntax, $(CRATES))))))))
$(filter-out log, \
$(filter-out regex, \
$(filter-out regex_macros, \
$(filter-out getopts, \
$(filter-out time, \
$(filter-out syntax, $(CRATES)))))))))))))
COMPILER_DOC_CRATES := rustc rustc_trans rustc_borrowck rustc_resolve \
rustc_typeck rustc_driver syntax

Expand Down
3 changes: 2 additions & 1 deletion mk/docs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ LIB_DOC_DEP_$(1) = \
$$(RSINPUTS_$(1)) \
$$(RUSTDOC_EXE) \
$$(foreach dep,$$(RUST_DEPS_$(1)), \
$$(TLIB2_T_$(CFG_BUILD)_H_$(CFG_BUILD))/stamp.$$(dep) \
$$(TLIB2_T_$(CFG_BUILD)_H_$(CFG_BUILD))/stamp.$$(dep)) \
$$(foreach dep,$$(filter $$(DOC_CRATES), $$(RUST_DEPS_$(1))), \
doc/$$(dep)/)
else
LIB_DOC_DEP_$(1) = $$(CRATEFILE_$(1)) $$(RSINPUTS_$(1))
Expand Down
2 changes: 1 addition & 1 deletion mk/grammar.mk
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ $(BG):
$(Q)mkdir -p $(BG)

$(BG)RustLexer.class: $(BG) $(SG)RustLexer.g4
$(Q)$(CFG_ANTLR4) -o $(B)grammar $(SG)RustLexer.g4
$(Q)$(CFG_ANTLR4) -o $(BG) $(SG)RustLexer.g4
$(Q)$(CFG_JAVAC) -d $(BG) $(BG)RustLexer.java

check-build-lexer-verifier: $(BG)verify
Expand Down
8 changes: 0 additions & 8 deletions mk/tests.mk
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@ endif
TEST_LOG_FILE=tmp/check-stage$(1)-T-$(2)-H-$(3)-$(4).log
TEST_OK_FILE=tmp/check-stage$(1)-T-$(2)-H-$(3)-$(4).ok

# If we're sharding the testsuite between parallel testers,
# pass this argument along to the compiletest and crate test
# invocations.
ifdef TEST_SHARD
CTEST_TESTARGS += --test-shard=$(TEST_SHARD)
CRATE_TEST_EXTRA_ARGS += --test-shard=$(TEST_SHARD)
endif

define DEF_TARGET_COMMANDS

ifdef CFG_UNIXY_$(1)
Expand Down
112 changes: 56 additions & 56 deletions src/doc/guide-tasks.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
% The Rust Tasks and Communication Guide
% The Rust Threads and Communication Guide

**NOTE** This guide is badly out of date an needs to be rewritten.
**NOTE** This guide is badly out of date and needs to be rewritten.

# Introduction

Expand All @@ -9,36 +9,36 @@ primitives. This guide will describe the concurrency model in Rust, how it
relates to the Rust type system, and introduce the fundamental library
abstractions for constructing concurrent programs.

Tasks provide failure isolation and recovery. When a fatal error occurs in Rust
Threads provide failure isolation and recovery. When a fatal error occurs in Rust
code as a result of an explicit call to `panic!()`, an assertion failure, or
another invalid operation, the runtime system destroys the entire task. Unlike
another invalid operation, the runtime system destroys the entire thread. Unlike
in languages such as Java and C++, there is no way to `catch` an exception.
Instead, tasks may monitor each other to see if they panic.
Instead, threads may monitor each other to see if they panic.

Tasks use Rust's type system to provide strong memory safety guarantees. In
particular, the type system guarantees that tasks cannot induce a data race
Threads use Rust's type system to provide strong memory safety guarantees. In
particular, the type system guarantees that threads cannot induce a data race
from shared mutable state.

# Basics

At its simplest, creating a task is a matter of calling the `spawn` function
with a closure argument. `spawn` executes the closure in the new task.
At its simplest, creating a thread is a matter of calling the `spawn` function
with a closure argument. `spawn` executes the closure in the new thread.

```{rust,ignore}
# use std::task::spawn;
# use std::thread::spawn;

// Print something profound in a different task using a named function
fn print_message() { println!("I am running in a different task!"); }
// Print something profound in a different thread using a named function
fn print_message() { println!("I am running in a different thread!"); }
spawn(print_message);

// Alternatively, use a `move ||` expression instead of a named function.
// `||` expressions evaluate to an unnamed closure. The `move` keyword
// indicates that the closure should take ownership of any variables it
// touches.
spawn(move || println!("I am also running in a different task!"));
spawn(move || println!("I am also running in a different thread!"));
```

In Rust, a task is not a concept that appears in the language semantics.
In Rust, a thread is not a concept that appears in the language semantics.
Instead, Rust's type system provides all the tools necessary to implement safe
concurrency: particularly, ownership. The language leaves the implementation
details to the standard library.
Expand All @@ -49,26 +49,26 @@ argument a closure (of type `F`) that it will run exactly once. This
closure is limited to capturing `Send`-able data from its environment
(that is, data which is deeply owned). Limiting the closure to `Send`
ensures that `spawn` can safely move the entire closure and all its
associated state into an entirely different task for execution.
associated state into an entirely different thread for execution.

```{rust,ignore}
# use std::task::spawn;
# fn generate_task_number() -> int { 0 }
# use std::thread::spawn;
# fn generate_thread_number() -> int { 0 }
// Generate some state locally
let child_task_number = generate_task_number();
let child_thread_number = generate_thread_number();

spawn(move || {
// Capture it in the remote task. The `move` keyword indicates
// that this closure should move `child_task_number` into its
// Capture it in the remote thread. The `move` keyword indicates
// that this closure should move `child_thread_number` into its
// environment, rather than capturing a reference into the
// enclosing stack frame.
println!("I am child number {}", child_task_number);
println!("I am child number {}", child_thread_number);
});
```

## Communication

Now that we have spawned a new task, it would be nice if we could communicate
Now that we have spawned a new thread, it would be nice if we could communicate
with it. For this, we use *channels*. A channel is simply a pair of endpoints:
one for sending messages and another for receiving messages.

Expand All @@ -78,7 +78,7 @@ of a channel, and a **receiver** is the receiving endpoint. Consider the followi
example of calculating two results concurrently:

```{rust,ignore}
# use std::task::spawn;
# use std::thread::spawn;

let (tx, rx): (Sender<int>, Receiver<int>) = channel();

Expand All @@ -102,12 +102,12 @@ into its component parts).
let (tx, rx): (Sender<int>, Receiver<int>) = channel();
```

The child task will use the sender to send data to the parent task, which will
The child thread will use the sender to send data to the parent thread, which will
wait to receive the data on the receiver. The next statement spawns the child
task.
thread.

```{rust,ignore}
# use std::task::spawn;
# use std::thread::spawn;
# fn some_expensive_computation() -> int { 42 }
# let (tx, rx) = channel();
spawn(move || {
Expand All @@ -116,10 +116,10 @@ spawn(move || {
});
```

Notice that the creation of the task closure transfers `tx` to the child task
Notice that the creation of the thread closure transfers `tx` to the child thread
implicitly: the closure captures `tx` in its environment. Both `Sender` and
`Receiver` are sendable types and may be captured into tasks or otherwise
transferred between them. In the example, the child task runs an expensive
`Receiver` are sendable types and may be captured into threads or otherwise
transferred between them. In the example, the child thread runs an expensive
computation, then sends the result over the captured channel.

Finally, the parent continues with some other expensive computation, then waits
Expand All @@ -137,7 +137,7 @@ The `Sender` and `Receiver` pair created by `channel` enables efficient
communication between a single sender and a single receiver, but multiple
senders cannot use a single `Sender` value, and multiple receivers cannot use a
single `Receiver` value. What if our example needed to compute multiple
results across a number of tasks? The following program is ill-typed:
results across a number of threads? The following program is ill-typed:

```{rust,ignore}
# fn some_expensive_computation() -> int { 42 }
Expand All @@ -160,7 +160,7 @@ Instead we can clone the `tx`, which allows for multiple senders.
let (tx, rx) = channel();

for init_val in range(0u, 3) {
// Create a new channel handle to distribute to the child task
// Create a new channel handle to distribute to the child thread
let child_tx = tx.clone();
spawn(move || {
child_tx.send(some_expensive_computation(init_val));
Expand All @@ -172,7 +172,7 @@ let result = rx.recv() + rx.recv() + rx.recv();
```

Cloning a `Sender` produces a new handle to the same channel, allowing multiple
tasks to send data to a single receiver. It upgrades the channel internally in
threads to send data to a single receiver. It upgrades the channel internally in
order to allow this functionality, which means that channels that are not
cloned can avoid the overhead required to handle multiple senders. But this
fact has no bearing on the channel's usage: the upgrade is transparent.
Expand All @@ -182,9 +182,9 @@ simply use three `Sender` pairs, but it serves to illustrate the point. For
reference, written with multiple streams, it might look like the example below.

```{rust,ignore}
# use std::task::spawn;
# use std::thread::spawn;

// Create a vector of ports, one for each child task
// Create a vector of ports, one for each child thread
let rxs = Vec::from_fn(3, |init_val| {
let (tx, rx) = channel();
spawn(move || {
Expand Down Expand Up @@ -256,18 +256,18 @@ fn main() {

## Sharing without copying: Arc

To share data between tasks, a first approach would be to only use channel as
To share data between threads, a first approach would be to only use channel as
we have seen previously. A copy of the data to share would then be made for
each task. In some cases, this would add up to a significant amount of wasted
each thread. In some cases, this would add up to a significant amount of wasted
memory and would require copying the same data more than necessary.

To tackle this issue, one can use an Atomically Reference Counted wrapper
(`Arc`) as implemented in the `sync` library of Rust. With an Arc, the data
will no longer be copied for each task. The Arc acts as a reference to the
will no longer be copied for each thread. The Arc acts as a reference to the
shared data and only this reference is shared and cloned.

Here is a small example showing how to use Arcs. We wish to run concurrently
several computations on a single large vector of floats. Each task needs the
several computations on a single large vector of floats. Each thread needs the
full vector to perform its duty.

```{rust,ignore}
Expand All @@ -284,10 +284,10 @@ fn main() {
let numbers_arc = Arc::new(numbers);

for num in range(1u, 10) {
let task_numbers = numbers_arc.clone();
let thread_numbers = numbers_arc.clone();

spawn(move || {
println!("{}-norm = {}", num, pnorm(task_numbers.as_slice(), num));
println!("{}-norm = {}", num, pnorm(thread_numbers.as_slice(), num));
});
}
}
Expand All @@ -306,8 +306,8 @@ let numbers_arc = Arc::new(numbers);
# }
```

and a clone is captured for each task via a procedure. This only copies
the wrapper and not its contents. Within the task's procedure, the captured
and a clone is captured for each thread via a procedure. This only copies
the wrapper and not its contents. Within the thread's procedure, the captured
Arc reference can be used as a shared reference to the underlying vector as
if it were local.

Expand All @@ -319,29 +319,29 @@ if it were local.
# let numbers=Vec::from_fn(1000000, |_| rand::random::<f64>());
# let numbers_arc = Arc::new(numbers);
# let num = 4;
let task_numbers = numbers_arc.clone();
let thread_numbers = numbers_arc.clone();
spawn(move || {
// Capture task_numbers and use it as if it was the underlying vector
println!("{}-norm = {}", num, pnorm(task_numbers.as_slice(), num));
// Capture thread_numbers and use it as if it was the underlying vector
println!("{}-norm = {}", num, pnorm(thread_numbers.as_slice(), num));
});
# }
```

# Handling task panics
# Handling thread panics

Rust has a built-in mechanism for raising exceptions. The `panic!()` macro
(which can also be written with an error string as an argument: `panic!(
~reason)`) and the `assert!` construct (which effectively calls `panic!()` if a
boolean expression is false) are both ways to raise exceptions. When a task
raises an exception, the task unwinds its stack—running destructors and
boolean expression is false) are both ways to raise exceptions. When a thread
raises an exception, the thread unwinds its stack—running destructors and
freeing memory along the way—and then exits. Unlike exceptions in C++,
exceptions in Rust are unrecoverable within a single task: once a task panics,
exceptions in Rust are unrecoverable within a single thread: once a thread panics,
there is no way to "catch" the exception.

While it isn't possible for a task to recover from panicking, tasks may notify
While it isn't possible for a thread to recover from panicking, threads may notify
each other if they panic. The simplest way of handling a panic is with the
`try` function, which is similar to `spawn`, but immediately blocks and waits
for the child task to finish. `try` returns a value of type
for the child thread to finish. `try` returns a value of type
`Result<T, Box<Any + Send>>`. `Result` is an `enum` type with two variants:
`Ok` and `Err`. In this case, because the type arguments to `Result` are `int`
and `()`, callers can pattern-match on a result to check whether it's an `Ok`
Expand All @@ -364,19 +364,19 @@ assert!(result.is_err());

Unlike `spawn`, the function spawned using `try` may return a value, which
`try` will dutifully propagate back to the caller in a [`Result`] enum. If the
child task terminates successfully, `try` will return an `Ok` result; if the
child task panics, `try` will return an `Error` result.
child thread terminates successfully, `try` will return an `Ok` result; if the
child thread panics, `try` will return an `Error` result.

[`Result`]: std/result/index.html

> *Note:* A panicked task does not currently produce a useful error
> *Note:* A panicked thread does not currently produce a useful error
> value (`try` always returns `Err(())`). In the
> future, it may be possible for tasks to intercept the value passed to
> future, it may be possible for threads to intercept the value passed to
> `panic!()`.

But not all panics are created equal. In some cases you might need to abort
the entire program (perhaps you're writing an assert which, if it trips,
indicates an unrecoverable logic error); in other cases you might want to
contain the panic at a certain boundary (perhaps a small piece of input from
the outside world, which you happen to be processing in parallel, is malformed
such that the processing task cannot proceed).
such that the processing thread cannot proceed).
Loading