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

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

1    use std::rc::Rc;
2
3 // The ball will survive until all kids are done playing with
it
4 struct Kid {
5 ball: Rc<Ball>,
6 }
7 struct Ball;
8
9 fn main() {
10 {
11 // rc is created and count is at 1
12 let foo = Rc::new("foo");
13 // foo goes out of scope; count decreases
14 // count is zero; the object gets destroyed
15 }
16
17 {
18 // rc is created and count is at 1
19 let bar = Rc::new("bar");
20 // rc is cloned; count increases to 2
21 let second_bar = Rc::clone(&bar);
22 // bar goes out of scode; count decreases to 1
23 // bar goes out of scode; count decreases to 0
24 }
25
26 {
27 // rc is created and count is at 1
28 let baz = Rc::new("baz");
29 {
30 // rc is cloned; count increases to...