How it works...

Since rayon is an external crate, we first downloaded it using the Cargo tool from the crates.io repo site. We used the latest version of rayon, which is 0.6.0. rayon currently requires rustc 1.12.0 and is available in crates.io. And note, rayon is an experimental crate.

rayon has an API called parallel iterators that lets us write iterator processes and execute them in parallel. We implemented the sum_of_squares function using parallel iterators.
To use parallel iterators, we first imported the traits by calling use rayon::prelude::* to our Rust module. We then called par_iter to get a parallel iterator, which is similar to a regular iterator. Parallel iterators work by first constructing a computation and then executing it; input is the parameter of the sum_of_squares function that returns an integer output. We performed the mathematical operation using the map and sum methods.

In the main function, we created a variable named rand_var and assigned it the value 10, which we passed as an argument to the sum_of_squares function. The return value is stored in the sum_sq variable and printed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.17.137