Book Image

Rust Standard Library Cookbook

By : Jan Hohenheim, Daniel Durante
Book Image

Rust Standard Library Cookbook

By: Jan Hohenheim, Daniel Durante

Overview of this book

Mozilla’s Rust is gaining much attention with amazing features and a powerful library. This book will take you through varied recipes to teach you how to leverage the Standard library to implement efficient solutions. The book begins with a brief look at the basic modules of the Standard library and collections. From here, the recipes will cover packages that support file/directory handling and interaction through parsing. You will learn about packages related to advanced data structures, error handling, and networking. You will also learn to work with futures and experimental nightly features. The book also covers the most relevant external crates in Rust. By the end of the book, you will be proficient at using the Rust Standard library.
Table of Contents (12 chapters)

How to do it...

  1. In the src/bin folder, create a file called parallelism.rs

  2. Add the following code and run it with cargo run --bin parallelism

1   use std::thread;
2
3 fn main() {
4 // Spawning a thread lets it execute a lambda
5 let child = thread::spawn(|| println!("Hello from a new
thread!"));
6 println!("Hello from the main thread!");
7 // Joining a child thread with the main thread means
8 // that the main thread waits until the child has
9 // finished it's work
10 child.join().expect("Failed to join the child thread");
11
12 let sum = parallel_sum(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
13 println!("The sum of the numbers 1 to 10 is {}", sum);
14 }
15
16 // We are going to write a function that
17 // sums the numbers in a slice in parallel
18 fn parallel_sum(range: &[i32]) -> i32 {
19 // We are going to use exactly 4 threads to sum the numbers
20 const NUM_THREADS: usize = 4;
21
22 // If we have less numbers than threads,
23 // there's no point in multithreading them
24 if range.len() < NUM_THREADS {
25 sum_bucket(range)
26 } else {
27 // We define "bucket" as the amount of numbers
28 // we sum in a single thread
29 let bucket_size = range.len() / NUM_THREADS;
30 let mut count = 0;
31 // This vector will keep track of our threads
32 let mut threads = Vec::new();
33 // We try to sum as much as possible in other threads
34 while count + bucket_size < range.len() {
35 let bucket = range[count..count +
bucket_size].to_vec();
36 let thread = thread::Builder::new()
37 .name("calculation".to_string())
38 .spawn(move || sum_bucket(&bucket))
39 .expect("Failed to create the thread");
40 threads.push(thread);
41
42 count += bucket_size
43 }
44 // We are going to sum the rest in the main thread
45 let mut sum = sum_bucket(&range[count..]);
46
47 // Time to add the results up
48 for thread in threads {
49 sum += thread.join().expect("Failed to join thread");
50 }
51 sum
52 }
53 }
54
55 // This is the function that will be executed in the threads
56 fn sum_bucket(range: &[i32]) -> i32 {
57 let mut sum = 0;
58 for num in range {
59 sum += *num;
60 }
61 sum
62 }