-
Book Overview & Buying
-
Table Of Contents
Rust for C++ Developers
By :
It's hard to avoid Rust if you spend much time reading about programming on social media. While it has been in stable release since 2015, among the myriad programming languages, it still feels young and perhaps unfamiliar to many. Yet companies such as Google and Microsoft have been vocal about embracing this language and its unique security and safety advantages.
Rust excels in many of the same areas as C++. Especially for low-overhead, high-performance programming, very few other languages compete with C and C++ as well as Rust. This alone makes learning it worthwhile for any C++ programmer looking to expand their technical toolbox.
This book is about Rust, but it's written specifically for C++ programmers. Throughout the book, we'll look at Rust through the lens of C++. We'll highlight the similarities and differences between the two languages, with an understanding that we can lean on our existing mastery of C++ to quickly understand the concepts Rust introduces.
The path we'll follow mirrors my own journey learning Rust. As a C++ professional for over two decades, I feel that my time with Rust has benefited me in several ways, including how I think about writing C++ code. I hope you'll come away from this book with a similar feeling.
This book is for C++ programmers who wish to learn about Rust: what it is, what it endeavors to solve, and why we'd want to use it in the first place. Every chapter is written specifically for C++ programmers, and thus, an intermediate to advanced knowledge of C++ will be helpful for our discussions comparing the two languages.
Chapter 1, Why Choose Rust?, gives us a brief introduction to the history of Rust and the technical trade-offs that have so many people excited about the language.
Chapter 2, Working with Rust Syntax, walks through the syntax of Rust, with each example compared directly to C++ where possible.
Chapter 3, The Rust Safety Model, shows exactly how Rust achieves its safety and security guarantees, which is fundamental for understanding how to use the language in practice.
Chapter 4, Managing Rust Projects with Cargo, explores Rust's excellent build, project setup, and dependency management tooling.
Chapter 5, Data Structures, explains how the Rust standard library represents common containers such as vectors, sets, and maps.
Chapter 6, Reading and Writing Files, reveals how Rust manages I/O operations and error handling when dealing with files and other kinds of I/O streams.
Chapter 7, Understanding Iterators, demystifies Rust's functional-style iterators and how they end up generating efficient code.
Chapter 8, Object Oriented Programming, shows Rust's unique take on object-oriented programming and explores how we can use Rust's functional and object-oriented features together in the same program.
Chapter 9, Working with C++ in Rust, explores interoperability between C++ and Rust, showing how the two languages can work together through Rust's foreign function interface.
Chapter 10, Optimization in Rust, details how we can find bottlenecks and measure improvement in Rust programs and explains how our intuitions about performance from C++ can be adapted to Rust.
Chapter 11, Multithreading in Rust, shows how Rust's safety guarantees extend to multithreaded programs and gives us several techniques for working with threads in Rust.
Chapter 12, Metaprogramming with Macros, discusses Rust's powerful macro system and works through examples of each of the many types of Rust macros.
Chapter 13, Continuing with Rust, provides information and suggestions about how to continue with Rust as a hobbyist, as a professional, and as a member of the community.
Appendix, Setting up a Development Environment, contains a step-by-step guide to setting up Visual Studio Code and a Rust development environment.
The version of Rust used in this book is 1.82.0. Code examples will work on Linux, macOS, or Windows. They can also be explored in the Rust Playground found at . Help installing Rust and a compatible text editor can be found in the Appendix.
The code bundle for the book is hosted on GitHub at https://github.com/PacktPublishing/Rust-for-CPP-Developers. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing. Check them out!
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://packt.link/gbp/9781836206514.
There are a number of text conventions used throughout this book.
CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. For example: "In recent standards, those languages have introduced the stdint.h and cstdint headers, providing fixed-width types such as int32_t or uint64_t."
A block of code is set as follows:
struct Vector3 {
x: f32,
y: f32,
z: f32,
}
Any command-line input or output is written as follows:
checkpoint 1
checkpoint 2
checkpoint 3
drop
checkpoint 4
Bold: Indicates a new term, an important word, or words that you see on the screen. For instance, words in menus or dialog boxes appear in the text like this. For example: "To avoid this, it's best to use Copy for structures that are conceptually "plain old data," or POD. "
Warnings or important notes appear like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book or have any general feedback, please email us at customercare@packt.com and mention the book's title in the subject of your message.
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you reported this to us. Please visit http://www.packt.com/submit-errata, click Submit Errata, and fill in the form.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit .
Once you've read Rust for C++ Developers, we'd love to hear your thoughts! Scan the QR code below to go straight to the Amazon review page for this book and share your feedback.

https://packt.link/r/1836206518
Your review is important to us and the tech community and will help us make sure we're delivering excellent quality content.