Contents In Detail

  1. Title Page
  2. Copyright
  3. About the Author
  4. Foreword
  5. Preface
  6. Acknowledgments
  7. Introduction
  8. Chapter 1: Foundations
    1. Talking About Memory
      1. Memory Terminology
      2. Variables in Depth
      3. Memory Regions
    2. Ownership
    3. Borrowing and Lifetimes
      1. Shared References
      2. Mutable References
      3. Interior Mutability
      4. Lifetimes
    4. Summary
  9. Chapter 2: Types
    1. Types in Memory
      1. Alignment
      2. Layout
      3. Complex Types
      4. Dynamically Sized Types and Wide Pointers
    2. Traits and Trait Bounds
      1. Compilation and Dispatch
      2. Generic Traits
      3. Coherence and the Orphan Rule
      4. Trait Bounds
      5. Marker Traits
    3. Existential Types
    4. Summary
  10. Chapter 3: Designing Interfaces
    1. Unsurprising
      1. Naming Practices
      2. Common Traits for Types
      3. Ergonomic Trait Implementations
      4. Wrapper Types
    2. Flexible
      1. Generic Arguments
      2. Object Safety
      3. Borrowed vs. Owned
      4. Fallible and Blocking Destructors
    3. Obvious
      1. Documentation
      2. Type System Guidance
    4. Constrained
      1. Type Modifications
      2. Trait Implementations
      3. Hidden Contracts
    5. Summary
  11. Chapter 4: Error Handling
    1. Representing Errors
      1. Enumeration
      2. Opaque Errors
      3. Special Error Cases
    2. Propagating Errors
    3. Summary
  12. Chapter 5: Project Structure
    1. Features
      1. Defining and Including Features
      2. Using Features in Your Crate
    2. Workspaces
    3. Project Configuration
      1. Crate Metadata
      2. Build Configuration
    4. Conditional Compilation
    5. Versioning
      1. Minimum Supported Rust Version
      2. Minimal Dependency Versions
      3. Changelogs
      4. Unreleased Versions
    6. Summary
  13. Chapter 6: Testing
    1. Rust Testing Mechanisms
      1. The Test Harness
      2. #[cfg(test)]
      3. Doctests
    2. Additional Testing Tools
      1. Linting
      2. Test Generation
      3. Test Augmentation
      4. Performance Testing
    3. Summary
  14. Chapter 7: Macros
    1. Declarative Macros
      1. When to Use Them
      2. How They Work
      3. How to Write Declarative Macros
    2. Procedural Macros
      1. Types of Procedural Macros
      2. The Cost of Procedural Macros
      3. So You Think You Want a Macro
      4. How Do They Work?
    3. Summary
  15. Chapter 8: Asynchronous Programming
    1. What’s the Deal with Asynchrony?
      1. Synchronous Interfaces
      2. Multithreading
      3. Asynchronous Interfaces
      4. Standardized Polling
    2. Ergonomic Futures
      1. async/await
      2. Pin and Unpin
    3. Going to Sleep
      1. Waking Up
      2. Fulfilling the Poll Contract
      3. Waking Is a Misnomer
      4. Tasks and Subexecutors
    4. Tying It All Together with spawn
    5. Summary
  16. Chapter 9: Unsafe Code
    1. The unsafe Keyword
    2. Great Power
      1. Juggling Raw Pointers
      2. Calling Unsafe Functions
      3. Implementing Unsafe Traits
    3. Great Responsibility
      1. What Can Go Wrong?
      2. Validity
      3. Panics
      4. Casting
      5. The Drop Check
    4. Coping with Fear
      1. Manage Unsafe Boundaries
      2. Read and Write Documentation
      3. Check Your Work
    5. Summary
  17. Chapter 10: Concurrency (and Parallelism)
    1. The Trouble with Concurrency
      1. Correctness
      2. Performance
    2. Concurrency Models
      1. Shared Memory
      2. Worker Pools
      3. Actors
    3. Asynchrony and Parallelism
    4. Lower-Level Concurrency
      1. Memory Operations
      2. Atomic Types
      3. Memory Ordering
      4. Compare and Exchange
      5. The Fetch Methods
    5. Sane Concurrency
      1. Start Simple
      2. Write Stress Tests
      3. Use Concurrency Testing Tools
    6. Summary
  18. Chapter 11: Foreign Function Interfaces
    1. Crossing Boundaries with extern
      1. Symbols
      2. Calling Conventions
    2. Types Across Language Boundaries
      1. Type Matching
      2. Allocations
      3. Callbacks
      4. Safety
    3. bindgen and Build Scripts
    4. Summary
  19. Chapter 12: Rust Without the Standard Library
    1. Opting Out of the Standard Library
    2. Dynamic Memory Allocation
    3. The Rust Runtime
      1. The Panic Handler
      2. Program Initialization
      3. The Out-of-Memory Handler
    4. Low-Level Memory Accesses
    5. Misuse-Resistant Hardware Abstraction
    6. Cross-Compilation
    7. Summary
  20. Chapter 13: The Rust Ecosystem
    1. What’s Out There?
      1. Tools
      2. Libraries
      3. Rust Tooling
      4. The Standard Library
    2. Patterns in the Wild
      1. Index Pointers
      2. Drop Guards
      3. Extension Traits
      4. Crate Preludes
    3. Staying Up to Date
    4. What Next?
      1. Learn by Watching
      2. Learn by Doing
      3. Learn by Reading
      4. Learn by Teaching
    5. Summary
  21. Index

List of Listings

  1. Listing 1-1: Values, variables, and pointers
  2. Listing 1-2: Illegal flows that the borrow checker will catch
  3. Listing 1-3: Moving and copying semantics
  4. Listing 1-4: Rust assumes that shared references are immutable.
  5. Listing 1-5: Rust assumes that mutable references are exclusive.
  6. Listing 1-6: Mutability applies only to the immediately referenced memory.
  7. Listing 1-7: Access through a mutable reference must leave a value behind.
  8. Listing 1-8: Lifetimes do not need to be contiguous.
  9. Listing 1-9: Lifetimes can have holes.
  10. Listing 1-10: A type that needs to be generic over multiple lifetimes
  11. Listing 1-11: A type that needs to be generic over multiple lifetimes
  12. Listing 2-1: Alignment affects layout.
  13. Listing 2-2: A generic method using static dispatch
  14. Listing 2-3: A generic method using dynamic dispatch
  15. Listing 2-4: Valid implementations of foreign traits for foreign types
  16. Listing 2-5: Invalid implementations of foreign traits for foreign types
  17. Listing 2-6: An excessively generic implementation of Debug for any iterable collection
  18. Listing 3-1: Similar function signatures with different contracts
  19. Listing 3-2: Using marker types to restrict implementations
  20. Listing 3-3: An innocent-looking public type
  21. Listing 3-4: User code accessing a single public field
  22. Listing 3-5: Implementing a trait for an existing type may cause problems.
  23. Listing 3-6: How to seal a trait and add implementations for it
  24. Listing 3-7: Re-exports make foreign crates part of your interface contract.
  25. Listing 3-8: Testing that a type implements a set of traits
  26. Listing 4-1: An enumerated error type
  27. Listing 4-2: A multi-step fallible function using the ? operator
  28. Listing 4-3: A multi-step fallible function that always cleans up after itself
  29. Listing 5-1: A feature that enables an optional dependency
  30. Listing 5-2: Enabling a feature of a dependency
  31. Listing 5-3: Adding and opting out of default features, and thus optional dependencies
  32. Listing 5-4: A workspace Cargo.toml
  33. Listing 5-5: Intercrate dependencies among workspace crates
  34. Listing 5-6: Overriding dependency sources in Cargo.toml using [patch]
  35. Listing 5-7: Overriding multiple versions of the same crate in Cargo.toml using [patch]
  36. Listing 5-8: Overriding profile options for a specific dependency or for a specific mode
  37. Listing 5-9: Conditional dependencies
  38. Listing 6-1: Opting out of the standard test harness
  39. Listing 6-2: A test that accesses inaccessible internal state and thus does not compile
  40. Listing 6-3: Using #[cfg(test)] to make internal state accessible in the testing context
  41. Listing 6-4: Using #[cfg(test)] to limit bookkeeping to the testing context
  42. Listing 6-5: Hiding lines in a doctest with #
  43. Listing 6-6: Testing that code fails to compile with compile_fail
  44. Listing 6-7: Fuzzing a URL parser with libfuzzer
  45. Listing 6-8: Wildly unsafe code that Miri detects is incorrect
  46. Listing 6-9: A suspiciously fast performance benchmark
  47. Listing 6-10: A corrected version of Listing 6-9
  48. Listing 6-11: What are we really benchmarking here?
  49. Listing 7-1: Repetitive testing code
  50. Listing 7-2: Making a macro repeat for you
  51. Listing 7-3: Using a macro to implement a trait for many similar types in one fell swoop
  52. Listing 7-4: Declarative macro definition components
  53. Listing 7-5: Macros exist in their own little universes. Mostly.
  54. Listing 7-6: Giving macros access to identifiers at the call site
  55. Listing 7-7: A very simple macro for implementing Debug
  56. Listing 8-1: The core of asynchrony: the “here you are or come back later” type
  57. Listing 8-2: A simplified view of the Future trait
  58. Listing 8-3: Implementing a channel-forwarding future using async and await
  59. Listing 8-4: Manually implementing a channel-forwarding future
  60. Listing 8-5: Implementing a channel-forwarding future using async and await, repeated from Listing 8-3
  61. Listing 8-6: Desugaring async/await into a generator
  62. Listing 8-7: Moving a future after polling it
  63. Listing 8-8: A less simplified view of the Future trait with Pin
  64. Listing 8-9: std::pin::Pin and its key methods
  65. Listing 8-10: The safe API to Pin for Unpin target types
  66. Listing 8-11: Macro for pinning to the stack
  67. Listing 8-12: A more accurate desugaring of <expr>.await
  68. Listing 8-13: The actual Future trait with Context
  69. Listing 8-14: Handling connections sequentially
  70. Listing 8-15: Handling connections with a manual executor
  71. Listing 8-16: Spawning futures to create more tasks that can be polled concurrently
  72. Listing 9-1: An unsafe method that contains only safe code
  73. Listing 9-2: A safe method that contains unsafe code
  74. Listing 9-3: Trying, and failing, to name the lifetime of a self-referential reference
  75. Listing 9-4: The GlobalAlloc trait with its required methods
  76. Listing 9-5: Using MaybeUninit to safely initialize an array
  77. Listing 9-6: A seemingly safe method for filling a vector with Default values
  78. Listing 9-7: Type layout is not predictable.
  79. Listing 9-8: Wrapper types make casting hard to get right.
  80. Listing 9-9: The implementation of Foo dictates whether this code should compile
  81. Listing 9-10: The implementations of both Foo and Bar dictate whether this code should compile
  82. Listing 9-11: A definition for Box that is maximally flexible in terms of the drop check
  83. Listing 10-1: The definition of Ordering
  84. Listing 10-2: Two racing threads with Ordering::Relaxed
  85. Listing 10-3: Listing 10-2 with Acquire/Release memory ordering
  86. Listing 10-4: Weird results with Acquire/Release ordering
  87. Listing 10-5: An incorrect implementation of a mutual exclusion lock
  88. Listing 10-6: A corrected implementation of a mutual exclusion lock
  89. Listing 11-1: Exposing a Rust static variable, and accessing one declared elsewhere, through FFI
  90. Listing 11-2: Exposing a Rust function, and accessing one defined elsewhere, through FFI
  91. Listing 11-3: Defining explicit variant values for a dataless enum
  92. Listing 11-4: Rust enums with #[repr(C)] are represented as tagged unions.
  93. Listing 11-5: An implementation-managed memory interface
  94. Listing 11-6: A caller-managed memory interface
  95. Listing 11-7: A library that expects single-threaded use
  96. Listing 11-8: Enforcing an FFI invariant by introducing auxiliary types
  97. Listing 11-9: Opaque pointer types that cannot be confused
  98. Listing 12-1: A heapless vector type
  99. Listing 12-2: Statically ensuring correct operation
  100. Listing 13-1: A representation of a combined input/output type that requires allocation
  101. Listing 13-2: A representation of a combined input/output type that does not require allocation
  102. Listing 13-3: Using a drop guard to ensure code gets run after an unwinding panic

Guide

  1. Cover
  2. Front Matter
  3. Foreword
  4. Preface
  5. Introduction
  6. Chapter 1: Foundations
  7. Start Reading
  8. Chapter 2: Types
  9. Chapter 3: Designing Interfaces
  10. Chapter 4: Error Handling
  11. Chapter 5: Project Structure
  12. Chapter 6: Testing
  13. Chapter 7: Macros
  14. Chapter 8: Asynchronous Programming
  15. Chapter 9: Unsafe Code
  16. Chapter 10: Concurrency (and Parallelism)
  17. Chapter 11: Foreign Function Interfaces
  18. Chapter 12: Rust Without the Standard Library
  19. Chapter 13: The Rust Ecosystem
  20. Index

Pages

  1. iii
  2. iv
  3. v
  4. xv
  5. xvi
  6. xvii
  7. xviii
  8. xix
  9. xx
  10. xxi
  11. xxii
  12. xxiii
  13. 1
  14. 2
  15. 3
  16. 4
  17. 5
  18. 6
  19. 7
  20. 8
  21. 9
  22. 10
  23. 11
  24. 12
  25. 13
  26. 14
  27. 15
  28. 16
  29. 17
  30. 19
  31. 20
  32. 21
  33. 22
  34. 23
  35. 24
  36. 25
  37. 26
  38. 27
  39. 28
  40. 29
  41. 30
  42. 31
  43. 32
  44. 33
  45. 34
  46. 35
  47. 37
  48. 38
  49. 39
  50. 40
  51. 41
  52. 42
  53. 43
  54. 44
  55. 45
  56. 46
  57. 47
  58. 48
  59. 49
  60. 50
  61. 51
  62. 52
  63. 53
  64. 54
  65. 55
  66. 56
  67. 57
  68. 58
  69. 59
  70. 60
  71. 61
  72. 62
  73. 63
  74. 64
  75. 65
  76. 67
  77. 68
  78. 69
  79. 70
  80. 71
  81. 72
  82. 73
  83. 74
  84. 75
  85. 76
  86. 77
  87. 78
  88. 79
  89. 80
  90. 81
  91. 82
  92. 83
  93. 84
  94. 85
  95. 86
  96. 87
  97. 88
  98. 89
  99. 90
  100. 91
  101. 92
  102. 93
  103. 94
  104. 95
  105. 96
  106. 97
  107. 98
  108. 99
  109. 100
  110. 101
  111. 102
  112. 103
  113. 104
  114. 105
  115. 106
  116. 107
  117. 108
  118. 109
  119. 110
  120. 111
  121. 112
  122. 113
  123. 114
  124. 115
  125. 117
  126. 118
  127. 119
  128. 120
  129. 121
  130. 122
  131. 123
  132. 124
  133. 125
  134. 126
  135. 127
  136. 128
  137. 129
  138. 130
  139. 131
  140. 132
  141. 133
  142. 134
  143. 135
  144. 136
  145. 137
  146. 138
  147. 139
  148. 140
  149. 141
  150. 142
  151. 143
  152. 144
  153. 145
  154. 146
  155. 147
  156. 148
  157. 149
  158. 150
  159. 151
  160. 152
  161. 153
  162. 154
  163. 155
  164. 156
  165. 157
  166. 158
  167. 159
  168. 160
  169. 161
  170. 162
  171. 163
  172. 164
  173. 165
  174. 166
  175. 167
  176. 168
  177. 169
  178. 170
  179. 171
  180. 172
  181. 173
  182. 174
  183. 175
  184. 176
  185. 177
  186. 178
  187. 179
  188. 180
  189. 181
  190. 182
  191. 183
  192. 184
  193. 185
  194. 186
  195. 187
  196. 188
  197. 189
  198. 190
  199. 191
  200. 192
  201. 193
  202. 194
  203. 195
  204. 196
  205. 197
  206. 198
  207. 199
  208. 200
  209. 201
  210. 202
  211. 203
  212. 204
  213. 205
  214. 206
  215. 207
  216. 208
  217. 209
  218. 211
  219. 212
  220. 213
  221. 214
  222. 215
  223. 216
  224. 217
  225. 218
  226. 219
  227. 220
  228. 221
  229. 222
  230. 223
  231. 224
  232. 225
  233. 226
  234. 227
  235. 228
  236. 229
  237. 230
  238. 231
  239. 232
  240. 233
  241. 234
  242. 235
  243. 236
  244. 237
  245. 238
  246. 239
  247. 240
  248. 241
  249. 242
  250. 243
  251. 245
  252. 246
  253. 247
  254. 248
  255. 249
  256. 250
  257. 251
  258. 252