Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Rust for C++ Developers
  • Table Of Contents Toc
Rust for C++ Developers

Rust for C++ Developers

By : Dan Olson
close
close
Rust for C++ Developers

Rust for C++ Developers

By: Dan Olson

Overview of this book

If you're a C++ programmer curious about the rising popularity of Rust, this book will guide you through the transition with clarity and purpose. Written by a veteran C++ developer who embraced Rust to improve software quality and maintainability, this hands-on guide shows you how to apply your existing knowledge to build efficient and safe systems with Rust. The first half of the book deep dives into Rust’s history, safety guarantees, and development tooling. From there, the book compares Rust and C++ side by side, covering syntax, SIMD instructions, file I/O, object orientation, and data structures. With each chapter, you’ll gain a practical understanding of Rust’s unique approaches—like ownership and borrowing—and how they solve long-standing challenges in C++. Later half of the book tackles performance optimization, multithreading, macros, and foreign function interfaces, culminating in a complete project where you reimplement a C++ program in Rust. By focusing on real-world code and familiar concepts, this book makes Rust accessible and actionable for experienced C++ developers. By the end of Rust for C++ Developers, you’ll be confident in your ability to read, write, and maintain production-grade Rust code, and you’ll have a clear roadmap for integrating Rust into your future projects.
Table of Contents (20 chapters)
close
close
Lock Free Chapter
1
Part 1: Understanding Rust Basics
6
Part 2: Exploring the Rust Standard Library
12
Part 3: Moving into Advanced Rust
18
Index

Preface

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.

Who this book is for

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.

What this book covers

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.

To get the most out of this book

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.

Download the example code files

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!

Download the color images

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.

Conventions used

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.

Get in touch

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 .

Share your thoughts

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.

Image 1

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.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Rust for C++ Developers
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon