Book Image

Rust Essentials - Second Edition

By : Ivo Balbaert
Book Image

Rust Essentials - Second Edition

By: Ivo Balbaert

Overview of this book

Rust is the new, open source, fast, and safe systems programming language for the 21st century, developed at Mozilla Research, and with a steadily growing community. It was created to solve the dilemma between high-level, slow code with minimal control over the system, and low-level, fast code with maximum system control. It is no longer necessary to learn C/C++ to develop resource intensive and low-level systems applications. This book will give you a head start to solve systems programming and application tasks with Rust. We start off with an argumentation of Rust's unique place in today's landscape of programming languages. You'll install Rust and learn how to work with its package manager Cargo. The various concepts are introduced step by step: variables, types, functions, and control structures to lay the groundwork. Then we explore more structured data such as strings, arrays, and enums, and you’ll see how pattern matching works. Throughout all this, we stress the unique ways of reasoning that the Rust compiler uses to produce safe code. Next we look at Rust's specific way of error handling, and the overall importance of traits in Rust code. The pillar of memory safety is treated in depth as we explore the various pointer kinds. Next, you’ll see how macros can simplify code generation, and how to compose bigger projects with modules and crates. Finally, you’ll discover how we can write safe concurrent code in Rust and interface with C programs, get a view of the Rust ecosystem, and explore the use of the standard library.
Table of Contents (13 chapters)

What this book covers

Chapter 1, Starting with Rust, discusses the main reasons that led to the development of Rust. We compare Rust with other languages and indicate the areas for which it is most appropriate. Then, we guide you through installing all the necessary components for a Rust development environment. In particular, you will learn how to work with Cargo, Rust’s package manager.

Chapter 2, Using Variables and Types, looks at the basic structure of a Rust program. We discuss the primitive types, how to declare variables and whether they have to be typed, and the scope of variables. Immutability, one of the key cornerstones of Rust’s safety strategy is also illustrated. Then, we look at basic operations, how to do formatted printing, and the important difference between expressions and statements.

Chapter 3, Using Functions and Control Structures, shows how to define functions, and the different ways to influence program execution flow in Rust. We also take a look at attributes and how to do testing in Rust.

Chapter 4, Structuring Data and Matching Patterns, discusses the basic data types for programming, such as strings, vectors, slices, tuples, and enums. You will learn how to get input from the console and how to work with program arguments. Then we show you the powerful pattern matching that is possible in Rust and how values are extracted by destructuring patterns.

Chapter 5, Higher Order Functions and Error-Handling, explores the functional features of Rust. We see how data structures and functions can be defined in a generic way. Furthermore, you will learn how to work with Rust’s unique error-handling mechanism.

Chapter 6, Using Traits and OOP in Rust, explores the object-oriented features of Rust. We see how traits can be used to define behavior and to simulate inheritance in data structures. We also explore some common OOP patterns implemented in Rust, such as the visitor and the builder pattern.

Chapter 7, Ensuring Memory Safety and Pointers, exposes the borrow checker, Rust’s mechanism to ensure that only memory safe operations can occur during program execution. Different kinds of pointers are discussed in this chapter.

Chapter 8, Organizing Code and Macros, discusses the bigger code-organizing structures in Rust, such as modules and crates. It also touches upon how to build macros in order to generate code, thus saving time and effort.

Chapter 9, Concurrency – Coding for Multicore Execution, delves into Rust’s concurrency model based on threads and channels. We also discuss a safe strategy for working with shared mutable data. 

Chapter 10, Programming at the Boundaries, looks at how Rust behaves in situations where we have to leave the safety boundaries, such as when interfacing with C or using raw pointers, and how Rust minimizes potential dangers when doing so.

Chapter 11, Exploring the Standard Library, gives us an overview of what is contained in Rust’s Standard Library with an emphasis on collections and the built-in macros. We also discuss how to let Rust work without standard library, for example, in very resource-constrained environments.

Chapter 12, The Ecosystem of Crates, covers how to work with crates built by other developers. We look at crates to work with files and databases, do web development, and develop graphics applications and games.