Incorporate Arc exercise from @ConnyOnny!! 🌟

This commit is contained in:
Carol (Nichols || Goulding) 2015-09-30 21:08:35 -04:00
parent 874a891592
commit 7af29d6211
2 changed files with 12 additions and 9 deletions

View File

@ -93,7 +93,7 @@ The [Error Handling](https://doc.rust-lang.org/stable/book/error-handling.html)
The [Concurrency](https://doc.rust-lang.org/stable/book/concurrency.html) section is relevant.
- "arc1.rs"
- ["arc1.rs"](http://play.rust-lang.org/?code=%2F%2F+Make+this+code+compile+by+filling+in+a+value+for+%60shared_numbers%60+where+the%0A%2F%2F+TODO+comment+is+and+creating+an+initial+binding+for+%60child_numbers%60%0A%2F%2F+somewhere.+Try+not+to+create+any+copies+of+the+%60numbers%60+Vec%21%0A%2F%2F+Scroll+down+for+hints+%3A%29%0A%0Ause+std%3A%3Async%3A%3AArc%3B%0Ause+std%3A%3Athread%3B%0A%0Afn+main%28%29+%7B%0A++++let+numbers%3A+Vec%3C_%3E+%3D+%280..100u32%29.collect%28%29%3B%0A++++let+shared_numbers+%3D+%2F%2F+TODO%0A++++let+mut+joinhandles+%3D+Vec%3A%3Anew%28%29%3B%0A%0A++++for+offset+in+0..8+%7B%0A++++++++joinhandles.push%28%0A++++++++thread%3A%3Aspawn%28move+%7C%7C+%7B%0A++++++++++++let+mut+i+%3D+offset%3B%0A++++++++++++let+mut+sum+%3D+0%3B%0A++++++++++++while+i+%3C+child_numbers.len%28%29+%7B%0A++++++++++++++++sum+%2B%3D+child_numbers%5Bi%5D%3B%0A++++++++++++++++i+%2B%3D+5%3B%0A++++++++++++%7D%0A++++++++++++println%21%28%22Sum+of+offset+%7B%7D+is+%7B%7D%22%2C+offset%2C+sum%29%3B%0A++++++++%7D%29%29%3B%0A++++%7D%0A++++for+handle+in+joinhandles.into_iter%28%29+%7B%0A++++++++handle.join%28%29.unwrap%28%29%3B%0A++++%7D%0A%7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%2F%2F+Make+%60shared_numbers%60+be+an+%60Arc%60+from+the+numbers+vector.+Then%2C+in+order%0A%2F%2F+to+avoid+creating+a+copy+of+%60numbers%60%2C+you%27ll+need+to+create+%60child_numbers%60%0A%2F%2F+inside+the+loop+but+still+in+the+main+thread.%0A%0A%2F%2F+%60child_numbers%60+should+be+a+clone+of+the+Arc+of+the+numbers+instead+of+a%0A%2F%2F+thread-local+copy+of+the+numbers.%0A)
### Threads

View File

@ -1,5 +1,7 @@
// make this code compile and don't create any copies of the "numbers" Vec.
// scroll down for hints
// Make this code compile by filling in a value for `shared_numbers` where the
// TODO comment is and creating an initial binding for `child_numbers`
// somewhere. Try not to create any copies of the `numbers` Vec!
// Scroll down for hints :)
use std::sync::Arc;
use std::thread;
@ -9,7 +11,7 @@ fn main() {
let shared_numbers = // TODO
let mut joinhandles = Vec::new();
for offset in 0..5 {
for offset in 0..8 {
joinhandles.push(
thread::spawn(move || {
let mut i = offset;
@ -45,8 +47,9 @@ fn main() {
// Make `shared_numbers` be an `Arc` from the numbers vector. Then, in order
// to avoid creating a copy of `numbers`, you'll need to create `child_numbers`
// inside the loop but still in the main thread.
// In line 6 you must create an Arc from the numbers vector.
// You must create the child_numbers inside the loop but still in the main thread.
// child_numbers is a clone of the Arc of the numbers, not a copy of the numbers.
// `child_numbers` should be a clone of the Arc of the numbers instead of a
// thread-local copy of the numbers.