@@ -442,21 +442,21 @@ It gives us this error:
442
442
```
443
443
444
444
It mentions that "numbers moved into closure environment". Because we referred
445
- to ` numbers ` inside of our ` proc ` , and we create ten ` proc ` s, we would have ten
446
- references. Rust detects this and gives us the error: we claim that ` numbers `
447
- has ownership, but our code tries to make ten owners. This may cause a safety
448
- problem, so Rust disallows it.
445
+ to ` numbers ` inside of our ` proc ` , and we create three ` proc ` s, we would have
446
+ three references. Rust detects this and gives us the error: we claim that
447
+ ` numbers ` has ownership, but our code tries to make three owners. This may
448
+ cause a safety problem, so Rust disallows it.
449
449
450
450
What to do here? Rust has two types that helps us: ` Arc<T> ` and ` Mutex<T> ` .
451
451
"Arc" stands for "atomically reference counted." In other words, an Arc will
452
452
keep track of the number of references to something, and not free the
453
453
associated resource until the count is zero. The 'atomic' portion refers to an
454
454
Arc's usage of concurrency primitives to atomically update the count, making it
455
- safe across threads. If we use an Arc, we can have our ten references. But, an
456
- Arc does not allow mutable borrows of the data it holds, and we want to modify
457
- what we're sharing. In this case, we can use a ` Mutex<T> ` inside of our Arc. A
458
- Mutex will synchronize our accesses, so that we can ensure that our mutation
459
- doesn't cause a data race.
455
+ safe across threads. If we use an Arc, we can have our three references. But,
456
+ an Arc does not allow mutable borrows of the data it holds, and we want to
457
+ modify what we're sharing. In this case, we can use a ` Mutex<T> ` inside of our
458
+ Arc. A Mutex will synchronize our accesses, so that we can ensure that our
459
+ mutation doesn't cause a data race.
460
460
461
461
Here's what using an Arc with a Mutex looks like:
462
462
0 commit comments