diff options
| author | Steve Klabnik <steve@steveklabnik.com> | 2015-03-22 15:36:24 -0400 |
|---|---|---|
| committer | Steve Klabnik <steve@steveklabnik.com> | 2015-03-22 15:40:46 -0400 |
| commit | 81801f2171d4a7dddbb9cbffe95e4afe93251cb7 (patch) | |
| tree | 18ec583c449784dd82ef6bdd7faed5eb1e797f92 /src/doc | |
| parent | ecf8c64e1b1b60f228f0c472c0b0dab4a5b5aa61 (diff) | |
| download | rust-81801f2171d4a7dddbb9cbffe95e4afe93251cb7.tar.gz rust-81801f2171d4a7dddbb9cbffe95e4afe93251cb7.zip | |
Re-word explanation on closures in intro
Fixes #23571
Diffstat (limited to 'src/doc')
| -rw-r--r-- | src/doc/intro.md | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/src/doc/intro.md b/src/doc/intro.md index 51280e58854..b711085ddbd 100644 --- a/src/doc/intro.md +++ b/src/doc/intro.md @@ -446,16 +446,16 @@ It gives us this error: error: aborting due to previous error ``` -It mentions that "captured outer variable in an `FnMut` closure". -Because we declared the closure as a moving closure, and it referred -to `numbers`, the closure will try to take ownership of the -vector. But the closure itself is created in a loop, and hence we will -actually create three closures, one for every iteration of the -loop. This means that all three of those closures would try to own -`numbers`, which is impossible -- `numbers` must have just one -owner. Rust detects this and gives us the error: we claim that -`numbers` has ownership, but our code tries to make three owners. This -may cause a safety problem, so Rust disallows it. +This is a little confusing because there are two closures here: the one passed +to `map`, and the one passed to `thread::scoped`. In this case, the closure for +`thread::scoped` is attempting to reference `numbers`, a `Vec<i32>`. This +closure is a `FnOnce` closure, as that’s what `thread::scoped` takes as an +argument. `FnOnce` closures take ownership of their environment. That’s fine, +but there’s one detail: because of `map`, we’re going to make three of these +closures. And since all three try to take ownership of `numbers`, that would be +a problem. That’s what it means by ‘cannot move out of captured outer +variable’: our `thread::scoped` closure wants to take ownership, and it can’t, +because the closure for `map` won’t let it. What to do here? Rust has two types that helps us: `Arc<T>` and `Mutex<T>`. *Arc* stands for "atomically reference counted". In other words, an Arc will |
