Book Image

Rust Programming Cookbook

By : Claus Matzinger
Book Image

Rust Programming Cookbook

By: Claus Matzinger

Overview of this book

Rust 2018, Rust's first major milestone since version 1.0, brings more advancement in the Rust language. The Rust Programming Cookbook is a practical guide to help you overcome challenges when writing Rust code. This Rust book covers recipes for configuring Rust for different environments and architectural designs, and provides solutions to practical problems. It will also take you through Rust's core concepts, enabling you to create efficient, high-performance applications that use features such as zero-cost abstractions and improved memory management. As you progress, you'll delve into more advanced topics, including channels and actors, for building scalable, production-grade applications, and even get to grips with error handling, macros, and modularization to write maintainable code. You will then learn how to overcome common roadblocks when using Rust for systems programming, IoT, web development, and network programming. Finally, you'll discover what Rust 2018 has to offer for embedded programmers. By the end of the book, you'll have learned how to build fast and safe applications and services using Rust.
Table of Contents (12 chapters)

What this book covers

Chapter 1, Starting off with Rust, covers how to set up Rust toolchains on your computer and the fundamental constructs of the language. These range from writing tests and benchmarks to language constructs such as loops, if expressions, traits, and structs.

Chapter 2, Going Further with Advanced Rust, answers questions about more in-depth features of the language as well as patterns for creating meaningful programs. Topics include borrowing and ownership in complex scenarios, the Option type, pattern matching, generics, explicit lifetimes, and enums.

Chapter 3, Managing Projects with Cargo, uses the cargo tool to manage additional crates, extensions, projects, and tests. You will find recipes that empower you to work on larger projects and solve challenges faced in managing them.

Chapter 4, Fearless Concurrency, goes into some best practices and recipes to build safe, fast programs. In addition to this, popular libraries such as Rayon are presented as we demonstrate that Rust is a great language for doing all kinds of concurrent tasks.

Chapter 5, Handling Errors and Other Results, explains how Rust uses the Result type and panics to perform error handling, integrating most failure cases into a regular workflow that needs to be treated. This chapter shows applicable patterns and best practices around avoiding unexpected crashes and unnecessary complexity.

Chapter 6, Expressing Yourself with Macros, explains how Rust's unique macro system extends the functionality of programs before compilation—in a type safe way. These macros can be implemented for many possible custom scenarios, and many crates use this ability. This chapter is all about creating useful macros to make your life easier and your programs safer.

Chapter 7, Integrating Rust with Other Languages, uses and works with different binary units and languages from within Rust in order to port legacy software or benefit from better SDKs. This is realized mostly through the Foreign Function Interface (FFI), which enables quick and easy integration with other native binaries. On top of that, it is possible to publish to npm (the Node.js package repository) from Rust using WebAssembly. These and other things are discussed in this chapter.

Chapter 8, Safe Programming for the Web, uses a state-of-the-art web framework to show the fundamentals of web programming – actix-web, which showcases an actor-based approach to handling requests, in productive use at Microsoft.

Chapter 9, Systems Programming Made Easy, explains how Rust is a great choice for running workloads on small devices with limited resources. In particular, the lack of a garbage collector and the resulting predictable runtime makes it a great fit for running sensor data collectors. Creating such a loop together with the required driver to read data is what we cover in this chapter.

Chapter 10, Getting Practical with Rust, covers practical considerations in Rust programming, such as parsing command-line arguments, working with neural networks (machine learning with PyTorch's C++ API), searching, regular expressions, web requests, and much more.