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 bin folder, create a file called

  2. Add the following code and run it with cargo test --bin interior_mutability:

1    trait EmailSender { 
2 fn send_mail(&self, msg: &Email) -> Option<String>;
3 }
5 #[derive(Debug, Clone)]
6 struct Email {
7 from: String,
8 to: String,
9 msg: String,
10 }
12 #[derive(Debug)]
13 struct Customer {
14 address: String,
15 wants_news: bool,
16 }
18 // Send news to every customer that wants to receive them
19 fn publish_news(msg: &str, sender: &EmailSender, customers: &
[Customer]) -> Option<i32> {
20 let mut count = 0;
21 let mut mail = Email {
22 from: "Rust Newsletter".to_string(),
23 to: "".to_string(),
24 msg: msg.to_string(),
25 };
26 for customer in customers {
27 if !customer.wants_news {
28 continue;
29 }
30 = customer.address.to_string();
31 if...