Rust for C Developers....2
Leverage your C expertise to write safer and faster systems code in Rust....2
Rust for C Developers....3
Contributors....5
About the author....5
About the reviewer....6
Table of Contents....8
Preface....18
Who this book is for....18
What this book covers....18
To get the most out of this book....19
Download the example code files....19
Download the color images....20
Conventions used....20
Get in touch....21
Share your thoughts....22
Free benefits with your book....23
How to Unlock....24
Part 1....26
Understanding Rust Basics....26
1....28
Why Choose Rust?....28
A brief history of Rust....29
Early beginnings at Mozilla....29
Stabilizing the language....30
Expanding into new territory....31
How Rust can improve software....32
High level, low level....32
Safety by default....34
Performance and memory....35
Modern language and tooling....35
Crates and the Rust ecosystem....36
The anatomy of a crate....36
Adding dependencies on community crates....37
Summary....38
Get this book's PDF version and more....38
2....40
Working with Rust Syntax....40
Technical requirements....40
Declaring variables and structures....41
Using simple variables....41
Learning array syntax....42
Working with structures....43
Using functions and methods....45
Functions and block expressions....45
Using methods to add behavior to structures....47
Calling functions and methods....49
Moving around the program....50
Using conditionals....50
Using loops and breaks....52
Understanding generic programming....54
Using generic structures....54
Specifying generic behavior with traits....56
Learning pattern matching....58
Using supercharged enumerations in Rust....58
Matching patterns....60
Summary....61
Get this book's PDF version and more....62
3....64
The Rust Safety Model....64
Technical requirements....64
Tracking ownership....65
The single ownership principle....65
Cleaning up old data....67
When shallow copies are enough....68
Borrowing data....70
Borrowing immutable data....70
Borrowing mutable data....71
Dereferencing borrows....73
Borrowing with method calls....74
Specifying lifetimes....74
Understanding lifetime annotations....74
Lifetimes in functions....75
Lifetimes in structures....76
Understanding panics....77
Panicking as a safety mechanism....77
Avoiding panics....78
Changing how we write code....80
Avoiding references in structures....80
Using copyable structures often....80
Learning not to fear cloning....81
Using indices instead of references....81
Summary....81
Get this book's PDF version and more....82
4....84
Managing Rust Projects with Cargo....84
Technical requirements....84
Structuring a Rust project....84
Creating a basic project....85
File structure of a crate....85
Understanding modules....86
Working with binaries....88
Working with dependencies....89
Exploring Rust's community crates....89
Managing dependencies with Cargo....89
Configuring dependencies....90
Adding non-Rust dependencies....91
Publishing crates....92
Setting up an account....92
Preparing a crate for publishing....93
Understanding semantic versioning....94
Sending a crate to the repository....95
Exploring Cargo's capabilities....95
Using Cargo as a test runner....95
Installing software with Cargo....96
Vendoring dependencies....98
Extending Cargo with plugins....98
Summary....99
Get this book's PDF version and more....99
Part 2....100
Exploring the Rust Standard Library....100
5....102
Data Structures....102
Technical requirements....102
Fundamentals of Rust data structures....103
Properties of strings in Rust....103
Owned and borrowed types....104
Converting between owned and borrowed types....105
Customizing dereference behavior in Rust....107
Working with indexed data....109
Using array types....109
Flexible runtime sizing with vectors....111
Working with borrowed data using slices....113
Using dictionary types....115
Flavors of map and set in Rust....115
Basic map and set usage....115
Using the entry API....117
Enabling custom key types....119
Summary....120
Get this book's PDF version and more....121
6....122
Reading and Writing Files....122
Technical requirements....122
Working with files....123
Using filesystem paths....123
Handling errors in Rust....124
Understanding high-level IO abstractions....127
Exploring the Rust IO API....128
Opening files for reading or writing....128
Understanding fundamental IO traits....130
Extending the IO APIs....134
Simple serialization and deserialization....136
Reading a JSON file using serde....136
Customizing serialization behavior....139
Writing out JSON....141
Summary....143
Get this book's PDF version and more....144
7....146
Understanding Iterators....146
Technical requirements....146
Defining and creating iterators....146
What is an iterator?....147
The properties of Rust iterators....148
Creating iterators....149
Consuming and composing iterators....152
Looping through iterators....152
Functions on the iterator trait....154
Composing iterators with adapters....155
Examining iterator performance....158
Dissecting an iterator expression....158
Vectorization optimizations....161
Summary....161
Get this book's PDF version and more....162
8....164
Object-Oriented Programming....164
Technical requirements....164
Understanding object-oriented principles....164
Abstracting behavior and encapsulating data....165
Modeling problems with inheritance and polymorphism....166
Examining object-oriented foundations in Rust....168
Implementation and data visibility....168
Composition over inheritance....170
Dynamic dispatch in Rust....173
Limitations of dynamic dispatch....175
Moving beyond object orientation....177
Modeling problems with Rust enums....177
Combining functional and object-oriented styles....179
Summary....180
Get this book's PDF version and more....181
9....182
Working with C in Rust....182
Technical requirements....182
Communicating between Rust and C....183
Understanding the C ABI....183
Declaring C-compatible structures in Rust....183
Calling C functions from Rust....184
Exposing Rust functions to C....187
Generating and updating bindings....189
Using bindgen to keep bindings up to date....189
Compiling and linking C libraries....191
Strategies for C binding....193
Manually creating bindings....193
Calling C directly from a Rust program....195
Using bindings effectively....197
Summary....200
Get this book's PDF version and more....200
Part 3....202
Moving into Advanced Rust....202
10....204
Optimization in Rust....204
Technical requirements....204
Understanding the performance of safe Rust....205
Aliasing....205
Initialization and bounds checking....206
Moving and cloning....207
Measuring the performance of Rust programs....208
Producing a test case....208
Annotating blocks of code....211
Sampling profilers....214
Benchmarking....216
Improving performance using unsafe Rust code....220
Removing bounds checks....220
Avoiding expensive initialization....222
Optimizing with SIMD instructions....224
Setting up nightly Rust....224
Writing a SIMD function....227
Summary....230
Get this book's PDF version and more....231
11....232
Multithreading in Rust....232
Technical requirements....232
Creating and managing threads....233
Spawning new threads....233
Scoped threads....235
Thread pooling....237
Multithreading safely in Rust....238
Understanding race conditions....238
Data races and the borrow checker....240
Synchronizing data across threads....242
Mutual exclusion....243
Atomic reference counting....244
Useful multithreading techniques....246
Parallel iterators with Rayon....246
Using channels....247
Atomics and lock-free programming....249
Summary....252
Get this book's PDF version and more....252
12....254
Metaprogramming with Macros....254
Technical requirements....254
Pitfalls of the preprocessor....255
Syntactic fragility....255
Variable capture....257
Declarative macros....258
Creating declarative macros in Rust....258
Creating our first macro....259
Expanding our capabilities with repetition....260
Matching explicit tokens....262
Practical considerations for declarative macros....262
Choosing delimiters....263
Implications of mixed-site hygiene....264
Procedural macros....266
Common properties of procedural macros....266
Custom derive....267
Attribute-like macros....272
Function-like macros....275
Macro best practices....276
Error handling....277
Debugging and testing....280
Summary....283
Get this book's PDF version and more....284
13....286
Continuing with Rust....286
Practicing Rust at home....286
Online challenges....287
Hobby projects....287
Navigating Rust in the workplace....289
Introducing Rust to professionals....289
Searching for a Rust career....290
Keeping up with Rust....290
Understanding the Rust community....291
Staying up to date with Rust....291
Summary....292
Get this book's PDF version and more....293
Appendix....294
Setting Up The Development Environment....294
Installing Rust development tools....294
Obtaining Rust development tools....295
Verifying installation....296
Managing a Rust installation with rustup....297
Setting up Visual Studio Code for Rust development....298
Installing Visual Studio Code....299
Using the Rust analysis extension....299
Debugging Rust in Visual Studio Code....301
Using Clippy, the Rust linter....302
Working with Clippy....302
Configuring Clippy for your project....303
Get this book's PDF version and more....305
15....306
Unlock Your Exclusive Benefits....306
Unlock this Book's Free Benefits in 3 Easy Steps....307
Step 1....307
Step 2....307
Step 3....308
Need Help....308
Why subscribe?....310
Other Books You May Enjoy....311
Packt is searching for authors like you....313
Share your thoughts....313
Index....314
Transition from C++ to Rust with ease by learning how to write safe, modern systems code using familiar examples
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.
Professional C++ developers are the primary audience for this book—especially those looking to improve software safety, maintainability, and concurrency handling in their systems code. It’s also ideal for systems programmers and engineers in fields like game development, embedded systems, and high-performance computing who are curious about Rust. Readers should be comfortable with C++ concepts such as pointers, memory allocation, and STL(Standard Template Library) containers. Familiarity with topics like concurrency, atomics, and debugging is beneficial but not required.