Parallelizing iterators
Wouldn't it be cool to have a magic button that allowed you to just make any algorithm parallel, without you doing anything? Well, as long as your algorithm uses iterators, rayon
is exactly that!
How to do it...
- Create a Rust project to work on during this chapter with
cargo new chapter-seven
. - Navigate into the newly-created
chapter-seven
folder. For the rest of this chapter, we will assume that your command line is currently in this directory. - Open the
Cargo.toml
file that has been generated for you. - Under
[dependencies]
, add the following line:
rayon = "1.0.0"
If you want, you can go to rayon
's crates.io page (https://crates.io/crates/rayon) to check for the newest version and use that one instead.
- Inside the
src
folder, create a new folder calledbin
. - Delete the generated
lib.rs
file, as we are not creating a library. - In the
src/bin
folder, create a file calledpar_iter.rs
. - Add the following code and run it with
cargo run --bin par_iter
:
1 extern crate rayon; 2 use rayon...