Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Rust Standard Library Cookbook
Rust Standard Library Cookbook

Rust Standard Library Cookbook: Over 75 recipes to leverage the power of Rust

Arrow left icon
Profile Icon Jan Hohenheim Profile Icon Daniel Durante
Arrow right icon
$48.99
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (1 Ratings)
Paperback Mar 2018 360 pages 1st Edition
eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m
Arrow left icon
Profile Icon Jan Hohenheim Profile Icon Daniel Durante
Arrow right icon
$48.99
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (1 Ratings)
Paperback Mar 2018 360 pages 1st Edition
eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m
eBook
$9.99 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Table of content icon View table of contents Preview book icon Preview Book

Rust Standard Library Cookbook

Working with Collections

In this chapter, we will cover the following recipes:

  • Using a vector
  • Using a string
  • Accessing collections as iterators
  • Using a VecDeque
  • Using a HashMap
  • Using a HashSet
  • Creating an own iterator
  • Using a slab

Introduction

Rust provides a very broad set of collections to use. We will look at most of them, see how they're used, discuss how they're implemented, and when to use and choose them. A big part of this chapter focuses on iterators. Much of Rust's flexibility comes from them, as all collections (and more!) can be used as iterators. Learning how to use them is crucial.

Throughout this chapter, we are going to use the big O notation to show how effective certain algorithms are. In case you don't know it yet, it is a way of telling how much longer an algorithm takes when working with more elements. Let's look at it briefly.

means that an algorithm is going to take the same time, no matter how much data is stored in a collection. It doesn't tell us how fast exactly it is, just that it's not going to slow down with size. This is the realistic ideal for a function. A practical example for this is accessing the first number in an infinite list of numbers...

Using a vector

The most basic collection is the vector, or Vec for short. It is essentially a variable-length array with a very low overhead. As such, it is the collection that you will use most of the time.

How to do it...

  1. In the command line, jump one folder up with cd .. so you're not in chapter-one anymore. In the next chapters, we are going to assume that you always started with this step.
  2. Create a Rust project to work on during this chapter with cargo new chapter-two.

  3. Navigate into the newly-created chapter-two folder. For the rest of this chapter, we will assume that your command line is currently in this directory.
  4. Inside the folder src, create a new folder called bin.
  5. Delete the generated lib.rs file, as we are not creating a library.
  6. In the folder src/bin, create a file called vector.rs.
  7. Add the following code blocks to the file and run them with cargo run --bin vector:
1  fn main() {
2 // Create a vector with some elements
3 let fruits = vec!["apple", "tomato", "pear"];
4 // A vector cannot be directly printed
5 // But we can debug-print it
6 println!("fruits: {:?}", fruits);
7
8 // Create an empty vector and fill it
9...

How it works...

This recipe is going to be a bit longer than the others, because:

  • The vector is the most important collection
  • Many of its core principles, like preallocation, apply to other collections as well
  • It includes methods used on slices, which are also usable by many other collections

Let's start at the beginning.

A vector can be created [9] by using the constructor pattern we mentioned earlier (Chapter 1, Learning the Basics, Using the Constructor Pattern), and filled by calling push on it for every element we want to store [10]. Because this is such a common pattern, Rust provides you with a convenient macro called vec![3]. While its end effect is the same, the macro is implemented with some nice performance optimizations.

Because of the convenience vec! provides, other Rustacians have implemented similar macros for the other collections, which you can find here: https://crates.io/crates/maplit.

If you want to initialize a vector by repeating an element over and over...

There's more...

The vector should always be your go-to collection. Internally, it is implemented as a continuous chunk of memory stored on the heap:

The important keyword here is continuous, which means that the memory is very cache-friendly. In other words, the vector is pretty fast! The vector even allocates a bit of extra memory in case you want to extend it. Be careful, though, when inserting a lot of data at the beginning of the vector: the entire stack will have to be moved.

At the end, you can see a bit of extra capacity. This is because Vec and many other collections preallocate a bit of extra memory each time you have to move the block, because it has grown too large. This is done in order to prevent as many reallocations as possible. You can check the exact amount of total space of a vector by calling capacity[140] on it. You can influence the preallocation by initializing your vector with with_capacity[137]. Use it when you have a rough idea about how many elements you...

Using a string

Rust provides an unusually large functionality for its string. Knowing it can save you quite some headache when dealing with raw user input.

How to do it...

  1. In the folder src/bin, create a file called string.rs.
  2. Add the following code, and run it with cargo run --bin string:
1   fn main() {
2 // As a String is a kind of vector,
3 // you can construct them the same way
4 let mut s = String::new();
5 s.push('H');
6 s.push('i');
7 println!("s: {}", s);
8
9 // The String however can also be constructed
10 // from a string slice (&str)
11 // The next two ways of doing to are equivalent
12 let s = "Hello".to_string();
13 println!("s: {}", s);
14 let s = String::from("Hello");
15 println!("s: {}", s);
16
17 // A String in Rust will always be valid UTF-8
18 let s = " Þjóðhildur ".to_string();
19 println!("s: {}", s);
20
21 // Append strings to each other
22 let mut s = "Hello ".to_string();
23 s.push_str("World");
24
25 // Iterate over the character
26 // A...

How it works...

Essentially, being a kind of vector, a string can be created the same way by combining new and push; however, because this is really inconvenient, a string, which is an owned chunk of memory, can be created from a string slice (&str), which is either a borrowed string or a literal. Both of the ways to do it, that are shown in this recipe, are equivalent:

    let s = "Hello".to_string();
println!("s: {}", s);
let s = String::from("Hello");
println!("s: {}", s);

Out of pure personal preference, we will use the first variant.

Before Rust 1.9, to_owned() was the fastest way to create a string. Now, to_string() is equally performant and should be preferred, because it offers more clarity over what is done. We mention this because many old tutorials and guides have not been updated since then, and still use to_owned().

All strings in Rust are valid Unicode in UTF-8 encoding. This can lead to some surprises, as a character...

There's more...

The implementation of String should not be much of a surpriseit's just a kind of vector:

Accessing collections as iterators

Welcome to one of the most flexible parts of the Rust standard library. Iterators are, as the name suggests, a way of applying actions of items in a collection. If you come from C#, you will already be familiar with iterators because of Linq. Rust's iterators are kind of similar, but come with a more functional approach to things.

Because they are an extremely fundamental part of the standard library, we are going to dedicate this recipe entirely to a showcase of all the different things you can do with them in isolation. For real-world use cases, you can simply continue reading the book, as a big portion of the other recipes features iterators in some way or another.

How to do it...

  1. In the folder src/bin, create a file called iterator.rs.
  2. Add the following code, and run it with cargo run --bin iterator:
1   fn main() {
2 let names = vec!["Joe", "Miranda", "Alice"];
3 // Iterators can be accessed in many ways.
4 // Nearly all collections implement .iter() for this purpose
5 let mut iter = names.iter();
6 // A string itself is not iterable, but its characters are
7 let mut alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".chars();
8 // Ranges are also (limited) iterators
9 let nums = 0..10;
10 // You can even create infinite iterators!
11 let all_nums = 0..;
12
13 // As the name says, you can iterate over iterators
14 // This will consume the iterator
15 for num in nums {
16 print!("{} ", num);
17 }
18 // nums is no longer usable
19 println!();
20
21 // Get the index of the current item
22 for (index, letter) in "abc".chars().enumerate() {
23 println...

How it works...

This recipe is incredibly important. No matter what you do, or which library you use, it's going to use iterators somewhere. All of the operations presented can be used on any collection and all types that implement the iterator trait.

In the first section, we looked at different ways to create iterators. I mention that ranges are limited because, in order to be iterable, the range-type has to implement Step. char doesn't, so you wouldn't be able to use 'A'..'D' as an iterator. For this reason, in line [209], we iterate over the characters as bytes:

    let alphabet: Vec<_> = (b'A' .. b'z' + 1) // Start as u8
.map(|c| c as char) // Convert all to chars
.filter(|c| c.is_alphabetic()) // Filter only alphabetic chars
.collect(); // Collect as Vec<char>

We have to set the limit of the range to b'z' + 1, because ranges are non-inclusive. You might have noticed that...

There's more...

iter() creates an iterator that borrows items. If you want to create an iterator that consumes itemsfor example, takes ownership of them by moving them  you can use into_iter().

See also

  • The Iterating over an inclusive range recipe in Chapter 10, Using Experimental Nightly Features

Using a VecDeque

When you need to insert or remove elements regularly into or from the beginning of the vector, your performance might take quite a hit, as it will force the vector to reallocate all data that comes after it. This is especially bothersome when implementing a queue. For this reason, Rust provides you with the VecDeque.

How to do it...

  1. In the folder src/bin, create a file called vecdeque.rs.
  2. Add the following code, and run it with cargo run --bin vecdeque:
1   use std::collections::VecDeque;
2
3 fn main() {
4 // A VecDeque is best thought of as a
5 // First-In-First-Out (FIFO) queue
6
7 // Usually, you will use it to push_back data
8 // and then remove it again with pop_front
9 let mut orders = VecDeque::new();
10 println!("A guest ordered oysters!");
11 orders.push_back("oysters");
12
13 println!("A guest ordered fish and chips!");
14 orders.push_back("fish and chips");
15
16 let prepared = orders.pop_front();
17 if let Some(prepared) = prepared {
18 println!("{} are ready", prepared);
19 }
20
21 println!("A guest ordered mozarella sticks!");
22 orders.push_back("mozarella sticks");
23
24 let prepared = orders.pop_front();
25 if let Some(prepared) = prepared {
26 println!("{} are...

How it works...

Most of the interface of VecDeque is identical to Vec. You can even optimize them the same way with with_capacity and its swap_remove equivalents. The differences come from the fact that VecDeque is more oriented around access from both ends. As such, multiple methods from Vec that implicitly affect the last element have two equivalents in VecDeque: one for the front, and one for the back. These are:

  • push, which becomes push_front [46] and push_back [11]
  • pop, which becomes pop_front [16] and pop_back [55]
  • swap_remove, which becomes remove_front [78] and remove_back [73]

A VecDeque has the ability to freely append or remove elements from both ends in a performant way, which makes it an ideal candidate for a First In, First Out (FIFO) queue [24]. In fact, this is how it's nearly always used.

When you see yourself in a situation where you want to respond to any kind of requests in the order they arrive in and remove them again afterwards, a VecDeque is an ideal tool...

There's more...

Internally, the VecDeque is implemented as a ring buffer, also known as a circular buffer. It's called like this because it behaves like a circle: the end touches the beginning.

It works by allocating a continuous block of memory, like the Vec; however, where the Vec always leaves its extra capacity at the end of the block, the VecDeque has nothing against leaving spots inside the block empty. It follows that when you remove the first element, the VecDeque doesn't move all elements to the left, but simply leaves the first spot empty. If you then push an element into the beginning via push_front, it will take the spot freed earlier while leaving the elements after it untouched.

The circular catch in the story is that if you have some capacity in the front of the block but none in the back while using push_back, the VecDeque will simply use that space to allocate the extra element, leading to the following situation:

This is great, because you will not...

Using a HashMap

If you imagine a Vec as a collection that assigns an index (0, 1, 2, and so on) to data, the HashMap is a collection that assigns any data to any data. It allows you to map arbitrary, hashable data to other arbitrary data. Hashing and mapping, that's where the name comes from!

How to do it...

  1. In the folder src/bin, create a file called hashmap.rs.
  2. Add the following code, and run it with cargo run --bin hashmap:
1   use std::collections::HashMap; 
2
3 fn main() {
4 // The HashMap can map any hashable type to any other
5 // The first type is called the "key"
6 // and the second one the "value"
7 let mut tv_ratings = HashMap::new();
8 // Here, we are mapping &str to i32
9 tv_ratings.insert("The IT Crowd", 8);
10 tv_ratings.insert("13 Reasons Why", 7);
11 tv_ratings.insert("House of Cards", 9);
12 tv_ratings.insert("Stranger Things", 8);
13 tv_ratings.insert("Breaking Bad", 10);
14
15 // Does a key exist?
16 let contains_tv_show = tv_ratings.contains_key("House of
Cards");
17 println!("Did we rate House of Cards? {}", contains_tv_show);
18 let contains_tv_show = tv_ratings.contains_key("House");
19 println!(&quot...

How it works...

As mentioned earlier, a HashMap is a collection to map one type of data to another. You do this by calling insert, and passing your key and its value [9]. If the key already had a value, it will be overwritten. This is why insert returns an Option: if there was a value before, it returns the old value [27], or otherwise None. If you want to make sure that you're not overwriting anything, make sure to check the result of contains_key [16] before inserting your value.

Both get and remove won't crash when called with an invalid key. Instead, they return a Result. In the case of remove, said Result contains the removed value.

As with most collections, you have the options to iterate over your data, by borrowing the key-value pairs[43], borrowing the keys while mutating the values [49], or moving them all [55]. Due to its nature, HashMap additionally allows you three more options: borrowing all values [74], mutating all values [80], or borrowing all keys [68]. You...

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

Using a HashSet

The best way to describe a HashSet is by describing how it's implemented: HashMap<K, ()>. It's just a HashMap without any values!

The two best reasons to choose a HashSet are:

  • You don't want to deal with duplicate values at all, as it doesn't even include them.
  • You plan on doing a lot (and I mean a lot) of item lookup - that is the question, Does my collection contain this particular item?. In a vector, this is done in , while a HashSet can do it in .

How to do it...

  1. In the folder src/bin, create a file called hashset.rs.
  2. Add the following code, and run it with cargo run --bin hashset:
1   use std::collections::HashSet;
2
3 fn main() {
4 // Most of the interface of HashSet
5 // is the same as HashMap, just without
6 // the methods that handle values
7 let mut books = HashSet::new();
8 books.insert("Harry Potter and the Philosopher's Stone");
9 books.insert("The Name of the Wind");
10 books.insert("A Game of Thrones");
11
12 // A HashSet will ignore duplicate entries
13 // but will return if an entry is new or not
14 let is_new = books.insert("The Lies of Locke Lamora");
15 if is_new {
16 println!("We've just added a new book!");
17 }
18
19 let is_new = books.insert("A Game of Thrones");
20 if !is_new {
21 println!("Sorry, we already had that book in store");
22 }
23
24 // Check if it contains a key
25 if...

How it works...

As a HashSet is a kind of HashMap, most of its interface is pretty similar. The major difference is that the methods that would return a key's value in the HashMap instead simply return a bool on the HashSet in order to tell if a key already existed or not [14].

Additionally, HashSet brings a few methods for analyzing two sets [46 to 92] and joining them [96 to 131]. If you've ever heard of set theory or Venn diagrams, or done a bit of SQL, you're going to recognize it all. Otherwise, I advise you to run the example, and study the outputs in combination with the relevant comments.

Some illustrations might help you. For the analytical methods, the dark green part is the object of reference:

For the selecting methods, the dark green part is the one that is returned:

There's more...

No big surprises in the implementation of the HashSet, since it's exactly the same as the HashMap, just without any values!

Creating an own iterator

When you create an infinitely applicable algorithm or a collection-like structure, it's really nice to have the dozens of methods that an iterator provides at your disposal. For this, you will have to know how to tell Rust to implement them for you.

How to do it...

  1. In the folder src/bin, create a file called own_iterator.rs.
  2. Add the following code, and run it with cargo run --bin own_iterator:
1   fn main() {
2 let fib: Vec<_> = fibonacci().take(10).collect();
3 println!("First 10 numbers of the fibonacci sequence: {:?}",
fib);
4
5 let mut squared_vec = SquaredVec::new();
6 squared_vec.push(1);
7 squared_vec.push(2);
8 squared_vec.push(3);
9 squared_vec.push(4);
10 for (index, num) in squared_vec.iter().enumerate() {
11 println!("{}^2 is {}", index + 1, num);
12 }
13 }
14
15
16 fn fibonacci() -> Fibonacci {
17 Fibonacci { curr: 0, next: 1 }
18 }
19 struct Fibonacci {
20 curr: u32,
21 next: u32,
22 }
23 // A custom iterator has to implement
24 // only one method: What comes next
25 impl Iterator for Fibonacci {
26 type Item = u32;
27 fn next(&mut self) -> Option<u32> {
28 let old = self.curr;
29 self.curr = self.next;
30 self.next +...

How it works...

In our little example here, we are going to look at two different uses for an iterator:

  • fibonacci(), which returns an infinite range of the Fibonacci sequence
  • SquaredVec, which implements a (very) small subset of a Vec with a twist: it squares all items
The Fibonacci sequence is defined as a series of numbers, starting from 0 and 1, where the next number is the sum of the last two. It starts like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, and so on.
The first two are 0 and 1 per definition. The next one is their sum — 0 + 1 = 1. After that comes 1 + 1 = 2. Then 2 + 1 = 3. 3 + 2 = 5. Repeat ad infinitum.

An algorithm can be turned into an iterator by implementing the Iterator trait. This is pretty simple, as it only expects you to provide the type you're iterating over and a single method, next, which fetches the next item. If the iterator doesn't have any items left, it should return None, otherwise Some. Our Fibonacci iterator always returns Some item...

There's more...

If you have a lot of complex logic to perform in the iterator and want to separate it a bit from your collection, you can do so by providing your collection with the IntoIterator trait instead. This would allow you to return a struct specifically made for your iteration, which itself provides the Iterator trait.

Using a slab

Some algorithms require you to hold access tokens to data that may or may not exist. This could be solved in Rust by using  Vec<Option<T>>, and treating the index of your data as a token. But we can do better! slab is an optimized abstraction of exactly this concept.

While it is not meant as a general-purpose collection, slab can help you a lot if you use it in the right places.

How to do it...

  1. Open the Cargo.toml file that has been generated earlier for you.

  2. Under [dependencies], add the following line:
slab = "0.4.0"
  1. If you want, you can go to slab's crates.io page (https://crates.io/crates/slab) to check for the newest version, and use that one instead.
  2. In the folder bin, create a file called slab.rs.

  3. Add the following code, and run it with cargo run --bin slab:

1   extern crate slab;
2 use slab::{Slab, VacantEntry};
3
4 fn main() {
5 // A slab is meant to be used as a limited buffer
6 // As such, you should initialize it with a pre-
7 // defined capacity
8 const CAPACITY: usize = 1024;
9 let mut slab = Slab::with_capacity(CAPACITY);
10
11 // You cannot simply access a slab's entry by
12 // index or by searching it. Instead, every
13 // insert gives you a key that you can use to
14 // access its entry
15 let hello_key = slab.insert("hello");
16 let world_key = slab.insert("world&quot...

How it works...

A slab is very similar to a vector, with one quintessential difference: you don't get to choose your index. Instead, when inserting data [15], you receive the data's index as a kind of key that you can use to access it again. It is your responsibility to store this key somewhere; otherwise, the only way to retrieve your data is by iterating over your slab. The flipside is that you don't have to provide any key either. In contrast to a HashMap, you don't need any hashable objects at all.

A situation in which this is useful is in a connection pool: if you have multiple clients who want to access individual resources, you can store said resources in a slab and provide the clients with their key as a kind of token.

This example suits the second use case of a slab really well. Suppose you only accept a certain amount of connections at a given time. When accepting a connection, you don't care about the exact index, or the way it is stored. Instead, you...

There's more...

A slab is backed by a Vec<Entry>. You might remember the Entry from our recipe about the HashMap earlier. It is the same as an Option, with the difference that its variants are not called Some(...) and None, but Occupied(...) and Vacant. This means that, in a nutshell, a slab is implemented as a vector with holes in it:

Additionally, in order to guarantee fast occupation of vacant spots, the slab keeps a linked list of all vacant entries.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Develop high-quality, fast, and portable applications by leveraging the power of Rust's Standard library.
  • Practical recipes that will help you work with the Standard library to boost your productivity as a Rust developer.
  • Learn about most relevant external crates to be used along with the Standard library.

Description

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.

Who is this book for?

This book is for developers who would like to explore the power of Rust and learn to use the STL for various functionalities. A basic Rust programming knowledge is assumed.

What you will learn

  • How to use the basic modules of the library: strings, command line access, and more.
  • Implement collections and folding of collections using vectors, Deque, linked lists, and more.
  • Handle various file types, compressing, and decompressing data.
  • Search for files with glob patterns.
  • Implement parsing through various formats such as CSV, TOML, and JSON.
  • Utilize drop trait, the Rust version of destructor.
  • Resource locking with Bilocks.
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Mar 29, 2018
Length: 360 pages
Edition : 1st
Language : English
ISBN-13 : 9781788623926
Category :
Languages :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to United States

Economy delivery 10 - 13 business days

Free $6.95

Premium delivery 6 - 9 business days

$21.95
(Includes tracking information)

Product Details

Publication date : Mar 29, 2018
Length: 360 pages
Edition : 1st
Language : English
ISBN-13 : 9781788623926
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 146.97
Rust Standard Library Cookbook
$48.99
Rust High Performance
$48.99
Network Programming with Rust
$48.99
Total $ 146.97 Stars icon
Banner background image

Table of Contents

11 Chapters
Learning the Basics Chevron down icon Chevron up icon
Working with Collections Chevron down icon Chevron up icon
Handling Files and the Filesystem Chevron down icon Chevron up icon
Serialization Chevron down icon Chevron up icon
Advanced Data Structures Chevron down icon Chevron up icon
Handling Errors Chevron down icon Chevron up icon
Parallelism and Rayon Chevron down icon Chevron up icon
Working with Futures Chevron down icon Chevron up icon
Networking Chevron down icon Chevron up icon
Using Experimental Nightly Features Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(1 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Donald A. Newell Jr. Mar 14, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Good reference book for Rust
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela