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

Wow! That was a lot of files. Of course, the exact content of the HTML and CSS doesn't matter for this recipe, as we're going to be focused on Rust. We've put them all in the files folder because we are going to make its contents publicly accessible by name for any client.

The basics of the server setup are the same as with the echoing recipe: create a Service with const_service and service_fn [21], match the request's method and path, and then handle the routes in different functions. When looking at our return type, however, we can notice a difference [36]:

type ResponseFuture = Box<Future<Item = Response, Error = hyper::Error>>;

We are no longer returning a Response directly, but instead, wrapping it in a Future. This allows us to not block the server when loading a file into memory; we can continue handling requests in the main thread while the file serving Future is run in the background.

When looking at our route handlers, you can...