Memory Thinking for Rust
Memory Thinking for Rust
Slides with Descriptions and Source Code Illustrations, Second Edition
About the Book
Memory Thinking for Rust reviews memory-related topics from the perspective of software structure and behavior analysis and teaches Rust language aspects in parallel while demonstrating relevant code internals on Windows (x64) and Linux (x64 and ARM64) platforms:
- Relevant language constructs
- Memory layout of structs and enums
- References, ownership, borrowing, and lifecycle
- Unsafe pointers
- Local, static, and dynamic memory
- Functions, closures
- Smart pointers
- Object-oriented and functional features
- Windows and Linux specifics
- … and much more
The new book edition updates and extends the existing topics, adding some missing from the first edition. The updated PDF book also has a new format similar to the second edition of Memory Thinking books for C and C++.
The book contains slides, brief notes highlighting particular points, and related source code with execution output. The following audiences may benefit from the book:
- Rust developers who want to deepen their knowledge
- Non-C and C++ developers (for example, Java, Scala, Python) who want to learn more about pointer and reference internals
- C and C++ developers who want to port their memory thinking to Rust quickly
Table of Contents
Table of Contents 3
Preface 9
About the Author 10
Introduction 11
Prerequisites 11
Training Goals 12
Warning 12
Training Principles 13
Schedule 13
Training Idea 14
General Rust Aspects 14
What We Do Not Cover 15
Linux Rust Aspects 16
Windows Rust Aspects 16
Why Rust? 17
My Genealogy of Rust 18
Rust Mastery Process 19
Thought Process 19
Philosophy of Unsafe Pointers 20
A General Pointer Concept 20
Pointer 21
Pointer Dereference 21
One to Many 22
Many to One 22
Many to One Dereference 23
Invalid Pointer 23
Invalid Pointer Dereference 24
Wild (Dangling) Pointer 24
Pointer to Pointer 25
Pointer to Pointer Dereference 25
Naming Pointers and Entities 26
Names as Pointer Content 26
Pointers as Entities 27
Unsafe Rust Code Examples 28
Pointer 29
Unsafe Pointer Dereference 30
One to Many 32
Memory Leak 34
Many to One 35
Unsafe Many to One Dereference 37
Invalid Pointer 38
Unsafe Invalid Pointer Dereference (Alignment) 39
Unsafe Invalid Pointer Dereference (Access Violation) 40
Unsafe Wild (Dangling) Pointer 41
Pointer to Pointer 42
Unsafe Pointer to Pointer Dereference 43
Philosophy of Values 45
Values and Owners 46
Moving Values 47
Copying Values 49
Dropping Values 50
Ownership Tree 52
Ownership Tree and Drops 53
Partial Drops 54
Ownership Tree and Moves 56
Multiple Owners (not in Rust) 57
Multiple Owners and Drops 58
Owners vs. Pointers 58
Rust: A Copernican Revolution 59
Values Revolve Around Pointers 59
Owners Revolve Around Values 60
Rust Philosophy of Values 61
Restricted Ownership 62
Value Lifetime 63
Owner Lifetime 65
Rust Philosophy of Pointers 68
Types of Pointers 69
Mut Pointers vs. Pointers to Mut 69
References as Pointer Types 70
Mut Refs vs. Refs to Mut 72
References as Addresses 73
Borrowing References 75
Reference Lifetime 77
x64 Disassembly Review (WinDbg) 79
x64 CPU Registers 79
Instructions and Registers 80
Memory and Stack Addressing 80
Memory Cell Sizes 81
Memory Load Instructions 81
Memory Store Instructions 82
Flow Instructions 82
Function Parameters 83
Struct Function Parameters 83
x64 Disassembly Review (GDB AT&T Flavor) 84
x64 CPU Registers 84
x64 Instructions and Registers 85
Memory and Stack Addressing 85
x64 Memory Load Instructions 86
x64 Memory Store Instructions 86
x64 Flow Instructions 87
x64 Function Parameters 87
x64 Struct Function Parameters 88
ARM64 Disassembly Review 89
A64 CPU Registers 89
A64 Instructions and Registers 90
Memory and Stack Addressing 90
A64 Memory Load Instructions 91
A64 Memory Store Instructions 91
A64 Flow Instructions 92
A64 Function Parameters 92
A64 Struct Function Parameters 93
Memory Storage 94
Memory Regions 95
Dynamic Virtual Memory 95
Static Memory 96
Rust Static Memory Values 96
Global vs. Local Static 97
Rust Static Memory References 97
Stack Memory 102
Thread Stack Frames 102
Local Value Lifecycle 103
Scope 103
Rust Stack Memory Values 104
Rust Stack Memory References 104
Heap Memory 107
Rust Heap Memory Values 108
Rust Const Values 110
Useful WinDbg Commands 112
Useful GDB Commands 112
Memory and Pointers 113
Mental Exercise 114
Debugger Memory Layout 114
Memory Dereference Layout 115
Names as Addresses 115
Addresses and Entities 116
Addresses and Structures 116
Pointers to Structures 117
Arrays 117
Arrays and Pointers to Arrays 118
Fat Pointers 118
Array Slices 119
String Literals (UTF-8) 122
Byte Strings 124
Vectors 125
Vector Slices 126
Strings 129
String Slices 130
C-Strings 132
C-String Slices 133
Basic Types 135
Bytes, Pointers, and References 136
u32, Pointers, and References 138
Little-Endian System 140
u64 and Pointers 141
Size 143
Alignment 147
Entity Conversion 149
Conversion through Pointers 150
Safe Conversion (Explicit Cast) 151
Safe Conversion (Coercion) 153
Forcing 155
Tuples 157
Structs 159
Tuple-like Structs 160
Newtypes 163
Newtypes (Binary Compatible) 165
Named-field Structs 166
Reference/Pointer to Struct 169
Ref/Ptr to Struct Dereference 170
Dereference with Replacement 171
One Ref/Ptr to Many Structs 173
Memory Leak 173
Many Ref/Ptr to One Struct 174
Many to One Dereference 174
Ref/Ptr to Ref/Ptr to Struct 175
Ref/Ptr to Ref/Ptr Dereference 175
Memory and Structs 179
Addresses and Structs 180
Struct Field Addresses 180
Ref/Ptr to Structs 183
Ref/Ptr to Struct and Fields 184
External Struct Alignment 187
Internal Struct Alignment (WinDbg) 187
Internal Struct Alignment (GDB) 188
Enums 191
Simple Enums 192
Enums with Structs 195
Enum Null Pointer Optimization 199
Source Code and Symbols 201
Conceptual Layer (Modules) 202
Logical Layer (Crates) 202
Physical Layer (Source Files) 203
Name Isolation 203
Functions 206
Pointers to Functions 207
References to Functions 209
Function Pointer Types 211
Struct Function Fields 212
Associated Functions 213
Pointers to Associated Functions 215
Type-associated Functions 217
Trait Functions 219
Trait Objects 219
vtable Memory Layout 221
Trait Object Memory Layout 221
Boxed Trait Object Layout 225
Struct Constructors 228
Struct Destructor 230
Struct Clone 234
Struct Copy 235
Parameters by Value 237
Parameters by Ref/Ptr 240
self 242
Trait Objects as Parameters 243
Struct as Return Value 244
Closures and Captures 246
Closure Struct 247
A64 Closure Struct Example 248
Captures (Borrowing) 248
Captures (Borrowing) x64 Linux 249
Captures (Move) 250
Captures (Move) x64 Linux 251
Smart Pointers 253
Why Smart Pointers? 254
Types of Smart Pointers 254
Interior Immutable Single Owner 255
Interior Mutable Single Ownership 257
Shared Ownership 261
Pinning 266
Use Cases 267
Rust Books 272
Other books by this author
The Leanpub 60 Day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.
You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!
So, there's no reason not to click the Add to Cart button, is there?
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $14 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them