Skip to content
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

Answers that circumvent the exercise #427

Closed
alexxroche opened this issue Jun 7, 2020 · 1 comment
Closed

Answers that circumvent the exercise #427

alexxroche opened this issue Jun 7, 2020 · 1 comment

Comments

@alexxroche
Copy link
Contributor

alexxroche commented Jun 7, 2020

I managed to get
✓ Successfully ran exercises/standard_library_types/arc1.rs!
which followed the letter of the exercise:

// Make this code compile by filling in a value for shared_numbers where the
// TODO comment is and create an initial binding for child_numbers somewhere.
// Try not to create any copies of the numbers Vec!

but failed the spirit of the exercise, (and ignored the hint - [flawed logic://{but why would I need the hint if I've already passed the exercise?}])

SPOILER: bad code, that compiles
use std::sync::Arc;
use std::thread;

fn main() {
    let numbers: Vec<_> = (0..100u32).collect();
    let shared_numbers = [12, 17, 9];
    let mut joinhandles = Vec::new();

    for offset in 0..8 {
        joinhandles.push(thread::spawn(move || {
            let mut i = offset;
            let mut sum = 0;
            //let child_numbers: Vec<u32> = [0; 100].to_vec(); // This is enough to compile, but circumvents the exercise of using Arc
            let child_numbers: Vec<_> = (0..100u32).collect(); // Same as previous line, but with values
            while i < child_numbers.len() {
                sum += child_numbers[i];
                i += 5;
            }
            println!("Sum of offset {} is {}", offset, sum);
        }));
    }
    for handle in joinhandles.into_iter() {
        handle.join().unwrap();
    }
}

The use std::sync::Arc; and filename of arc1.rs should be enough of a clue, but would it be helpful for rustlings to check that the topic being taught is actually being applied? (In this case, check that something is wrapped in an Arc?) Or at least have a soft, "// Must use std::sync::Arc;" added to the head of the file.

(Would the following be helpful, or too much?)

// I AM NOT DONE

#![forbid(unused_imports)] //Do not remove this, (or the next) line.
use std::sync::Arc;
@alexxroche
Copy link
Contributor Author

Pull-request #429 created, in the case that this is considered useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant