Book Image

Mastering Rust - Second Edition

By : Rahul Sharma, Vesa Kaihlavirta
Book Image

Mastering Rust - Second Edition

By: Rahul Sharma, Vesa Kaihlavirta

Overview of this book

Rust is an empowering language that provides a rare combination of safety, speed, and zero-cost abstractions. Mastering Rust – Second Edition is filled with clear and simple explanations of the language features along with real-world examples, showing you how you can build robust, scalable, and reliable programs. This second edition of the book improves upon the previous one and touches on all aspects that make Rust a great language. We have included the features from latest Rust 2018 edition such as the new module system, the smarter compiler, helpful error messages, and the stable procedural macros. You’ll learn how Rust can be used for systems programming, network programming, and even on the web. You’ll also learn techniques such as writing memory-safe code, building idiomatic Rust libraries, writing efficient asynchronous networking code, and advanced macros. The book contains a mix of theory and hands-on tasks so you acquire the skills as well as the knowledge, and it also provides exercises to hammer the concepts in. After reading this book, you will be able to implement Rust for your enterprise projects, write better tests and documentation, design for performance, and write idiomatic Rust code.
Table of Contents (19 chapters)

What this book covers

Chapter 1, Getting Started with Rust, gives a brief history on Rust and the motivation behind its design, and covers basic language syntax. The chapter ends with an exercise covering all the language features.

Chapter 2, Managing Projects with Cargo, shows how Rust organizes large projects with its dedicated package manager. This serves as the basis for further chapters. It also covers editor integration with the Visual Studio Code editor.

Chapter 3, Tests, Documentation and Benchmarks, explores the built-in testing harness, writing unit tests, integration tests, and how to write documentation in Rust. We also cover the benchmarking facilities of Rust code. Later, as a final exercise, we build a complete crate with documentation and tests.

Chapter 4, Types, Generics, and Traits, explores Rust's expressive type system and goes on to explain various ways of using the type system by building a complex number library.

Chapter 5, Memory Management and Safety, starts with the motivation for memory management and the various pitfalls in conventional low-level programming languages related to memory. It then moves toward explaining Rust's unique compile-time memory management ideas. We also explain various smart pointer types in Rust.

Chapter 6, Error Handling, starts with the motivation for error handling and explores different models of error handling in other languages. The chapter then examine Rust's error-handling strategy and types, before exploring handling errors in non-recoverable situations. The chapter ends with a library implementing custom error types.

Chapter 7, Advanced Concepts, explores some of the concepts already introduced in previous chapters, in more detail. It provides details on the underlying model of some of the type system abstractions provided by Rust.

Chapter 8, Concurrency, explores Rust's concurrency models and APIs in the standard libraries and teaches you how to build highly concurrent programs with no data races.

Chapter 9, Metaprogramming with Macros, examines how you can write code to generate code using the powerful and advanced macro construct of Rust, and outlines the language's declarative and procedural macros by building both types of macros.

Chapter 10, Unsafe Rust and Foreign Function Interfaces, explores the unsafe mode of Rust and the APIs on offer for interoperating Rust with other languages. The examples includes both calling into Rust from other languages, such as Python, Node.js, and C, as well as covering how Rust can be called from other languages.

Chapter 11, Logging, explains why logging is an important practice in software development, answering why we need logging frameworks, and exploring the crates on offer in the Rust ecosystem that can be used to help integrate logging into the application.

Chapter 12, Network Programming in Rust Sync and Async I/O, gives a brief introduction to network programming. After going through the basics, the chapter covers building a Redis server that can talk to the official Redis client. Lastly, the chapter explains how to use the standard library networking primitives and the Tokio and futures crates.

Chapter 13, Building Web Applications with Rust, starts by exploring the HTTP protocol and builds a simple URL shortener server using the hyper crate, followed by building a URL shortener client using the reqwest crate. In the end, we explore actix-web, a high-performance Async web application framework to build a bookmarks API server.

Chapter 14, Interacting with Databases in Rust, starts with the motivation on the need for database backend applications and moves toward exploring the available crates in the Rust ecosystem to interact with various database backends, such as SQLite and PostgreSQL. The chapter also explores a type-safe ORM crate called diesel. Later, it covers how to integrate our bookmarks API server, built in the previous chapter, to integrate database support using diesel.

Chapter 15, Rust on the Web Using WebAssembly, explains what WebAssembly is and how it can be used by developers. We then move on to exploring the available crates in the Rust ecosystem, and build a live markdown-editor web application using Rust and WebAssembly.

Chapter 16, Building Desktop Applications with Rust, explains how to build desktop applications with Rust using the GTK framework. We'll build a simple hacker-news desktop client.

Chapter 17, Debugging, explores debugging Rust code with GDB and also shows how to integrate GDB with Visual Studio Code editor.