If you've read Chapter 5, Advanced Data Structures; Boxing data, this recipe doesn't need much explanation. By returning an impl trait, we tell the caller of a function to not care about the specific struct that is returned, and that its only guarantee about it is that it implements some trait. In this sense, abstract return types work like the trait objects discussed in the said recipe, with the added bonus of being way faster, as they don't have any overhead. This is useful for returning iterators [37] and closures [43], which we adapted from the recipe about boxes, but also to hide implementation details. Consider our function create_animal[32]. A caller will only care that it returns a struct that implements Animal, but not which exact animal. If a Dog [7] doesn't prove to be the right thing because of changing requirements, you can create a Cat, and return that one without touching the rest of the code, as it all just depends on Animal. This is...
Rust Standard Library Cookbook
By :
Rust Standard Library Cookbook
By:
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)
Preface
Free Chapter
Learning the Basics
Working with Collections
Handling Files and the Filesystem
Serialization
Advanced Data Structures
Handling Errors
Parallelism and Rayon
Working with Futures
Networking
Using Experimental Nightly Features
Other Books You May Enjoy
Customer Reviews