Running two operations together
The parallel iterators from the last recipe are internally built upon a more fundamental function, rayon::join
, which takes two closures and potentially runs them in parallel. This way, even the balance of performance gain versus the overhead of spawning a thread has been done for you.
If you have an algorithm that doesn't use iterators but still consists of some clearly separated parts that could benefit from running concurrently, consider using rayon::join
for that.
How to do it...
Open the
Cargo.toml
file that was generated earlier for you.- If you didn't do so in the last recipe, 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. In the folder
bin
, create a file calledjoin.rs
.Add the following code and run it with
cargo run --bin join
:
1 extern crate rayon; 2 3 #[derive(Debug)] 4 struct Rectangle { 5 height...