-
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 called join.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: u32,
6 width: u32,
7 }
8
9 impl Rectangle {
10 fn area(&self) -> u32 {
11 self.height * self.width
12 }
13 fn perimeter(&self) -> u32 {
14 2 * (self.height + self.width)
15 }
16 }
17
18 fn main() {
19 let rect = Rectangle {
20 height: 30,
21 width: 20,
22 };
23 // rayon::join makes closures run potentially in parallel and
24 // returns their returned values in a tuple
25 let (area, perimeter) = rayon::join...