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)

To get the most out of this book

This book has been written with and tested for the Rust versions rustc 1.24.1 and rustc 1.26.0-nightly; however, Rust's strong backward compatibility should make it possible for you to use any newer versions for all chapters except the last. Chapter 10, Using Experimental Nightly Features, is working with cutting-edge technology that is expected to improve through ground-breaking changes.

To download the newest Rust version, visit, where you will be able to download a Rust installer for your operating system. It's okay to leave it at the standard settings. Make sure to call rustup default nightly before starting Chapter 10, Using Experimental Nightly Features. Don't worry, you'll be reminded again when it's time.

An active internet connection is required for many recipes, as we will work intensively with crates. These are Rust's way of distributing libraries over the internet, and they are hosted at

You might wonder why a book about Rust's standard library, or std for short, uses so much code from outside the std. That's because Rust, in contrast to most other system languages, was designed with strong dependency management in mind from the beginning. It's so easy to pull crates into your code that a lot of specific functionality has been outsourced to officially recommended crates. This helps the core standard library that is distributed with Rust to stay simple and very stable.

The most official group of crates after the std is the nursery ( These crates are the standard for many operations and are nearly stable or generic enough to be in the std.

If we can't find a crate for a recipe in the nursery, we look at the crates of the Rust core team members (, who put a lot of effort into providing functionality that is missing from the standard library. These crates are not in the nursery because they are usually specific enough that it is not worth allocating too many resources to actively maintaining them.

All the code in this book has been formatted with the newest rustfmt (rustfmt-nightly v0.4.0), which you can optionally download using rustup component add rustfmt-preview and run with cargo fmt. The code on GitHub ( is going to be actively maintained and consequently formatted using a newer version of rustfmt, if available. In some cases, this means that the source code line markings can become outdated. It should not be hard to find the code, however, as this shift is usually no greater than two or three lines.

All code has also been checked by Rust's official linter, clippy (, using version 0.0.187. If you want, you can install it with cargo +nightly install clippy and run it with cargo +nightly clippy. The newest version tends to break quite often though, so don't be surprised if it doesn't work outright.

Some clippy and rustc warnings have been left in the code intentionally. Most of these are either dead code, which happens when we assign a value to a variable to illustrate a concept and then don't need to use the variable anymore, or usage of placeholder names such as foo, bar, or baz, which are used when the exact purpose of a variable is irrelevant to the recipe.