How it works...
rayon-rs (https://github.com/rayon-rs/rayon) is a popular data-parallelism crate that only requires a few modifications to introduce automatic concurrency into the code. In our example, we are using the rayon::join operation to create a parallel version of the popular merge sort algorithm.
In step 1, we are adding dependencies for benchmarks ([dev-dependencies]) and to actually build the library ([dependencies]). But in step 2 and step 3, we are implementing a regular merge sort variation. Once we add the rayon dependency in step 4, we can add rayon::join in step 5 to run each branch (to sorting of the left and right parts) in its own closure (|/*no params*/| {/* do work */}, or |/*no params*/| /*do work*/ for short) in parallel ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access