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

Let's start by introducing the Future trait: 

  • Implementing the Future trait requires only three constraints: an Item  type, an Error type, and a poll() function. The actual trait looks as follows:
pub trait Future {
type Item;
type Error;
fn poll(
&mut self,
cx: &mut Context
) -> Result<Async<Self::Item>, Self::Error>;
  • The Poll<Self::Item, Self::Error> is a type that translates into Result<Async<T>, E> , where T = Item and E = Error. This is what our example is using on line 50.
  • poll() is called upon whenever a futures::task::Waker (can also be known as a Task) is executed with one of our executors located at futures::executor, or manually woken up by building a futures::task::Context and running with a future wrapper such as futures::future::poll_fn.

Now, onto our local_until() function:

  • LocalPool offers us...