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)

There's more...

Internally, you can imagine the HashMap as being implemented as two vectors: a table, and a buffer. Of course, we're simplifying here; there are actually no vectors in the implementation. But this analogy is accurate enough.

If you want to look at the actual implementation, feel free to do so, as Rust is completely open source: https://github.com/rust-lang/rust/blob/master/src/libstd/collections/hash/table.rs.

In the background, the buffer stores our values in a sequential fashion. In the front, we have a table storing buckets that don't do much more than point to the element they stand for. When you insert a key-value pair, what happens is:

  1. The value gets put in the buffer.
  2. The key goes through a hashing function and becomes an index.
  3. The table creates a bucket at said index that points to the actual value:
Rust's hashing algorithm doesn't actually generate unique indices, for performance reasons. Instead, Rust uses a clever way to handle hash...