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 it works...

A fundamental building block of parallelism in Rust is the Arc, which stands for Atomically Reference Counted. Functionally, it works the same way as an Rc, which we have looked at in Chapter 5, Advanced Data Structures; Sharing ownership with smart pointers. The only difference is that the reference counting is done using atomic primitives, which are versions of primitive data types like usize that have well-defined parallel interactions. This has two consequences:

  • An Arc is slightly slower than an Rc, as the reference counting involves a bit more work
  • An Arc can be used safely across threads

The constructor of Arc looks the same as Rc[7]:

let some_resource = Arc::new("Hello World".to_string());

This creates an Arc over a String. A String is a struct that is not inherently saved to be manipulated across threads. In Rust terms, we say that String is not Sync (more about that later in the recipe Atomically access primitives).

Now let's look at how a thread...