Skip to content

Commit bfcebbb

Browse files
committed
Adopt <Listing> for Ch. 20
1 parent 7335c65 commit bfcebbb

5 files changed

+95
-92
lines changed

src/ch20-01-unsafe-rust.md

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,13 @@ interface with another language or hardware where Rust’s guarantees don’t ap
9191
Listing 20-1 shows how to create an immutable and a mutable raw pointer from
9292
references.
9393

94+
<Listing number="20-1" caption="Creating raw pointers from references">
95+
9496
```rust
9597
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-01/src/main.rs:here}}
9698
```
9799

98-
<span class="caption">Listing 20-1: Creating raw pointers from references</span>
100+
</Listing>
99101

100102
Notice that we don’t include the `unsafe` keyword in this code. We can create
101103
raw pointers in safe code; we just can’t dereference raw pointers outside an
@@ -115,23 +117,25 @@ so there is no memory access, or the program might error with a segmentation
115117
fault. Usually, there is no good reason to write code like this, but it is
116118
possible.
117119

120+
<Listing number="20-2" caption="Creating a raw pointer to an arbitrary memory address">
121+
118122
```rust
119123
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-02/src/main.rs:here}}
120124
```
121125

122-
<span class="caption">Listing 20-2: Creating a raw pointer to an arbitrary
123-
memory address</span>
126+
</Listing>
124127

125128
Recall that we can create raw pointers in safe code, but we can’t *dereference*
126129
raw pointers and read the data being pointed to. In Listing 20-3, we use the
127130
dereference operator `*` on a raw pointer that requires an `unsafe` block.
128131

132+
<Listing number="20-3" caption="Dereferencing raw pointers within an `unsafe` block">
133+
129134
```rust
130135
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-03/src/main.rs:here}}
131136
```
132137

133-
<span class="caption">Listing 20-3: Dereferencing raw pointers within an
134-
`unsafe` block</span>
138+
</Listing>
135139

136140
Creating a pointer does no harm; it’s only when we try to access the value that
137141
it points at that we might end up dealing with an invalid value.
@@ -196,24 +200,26 @@ we might implement it. This safe method is defined on mutable slices: it takes
196200
one slice and makes it two by splitting the slice at the index given as an
197201
argument. Listing 20-4 shows how to use `split_at_mut`.
198202

203+
<Listing number="20-4" caption="Using the safe `split_at_mut` function">
204+
199205
```rust
200206
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-04/src/main.rs:here}}
201207
```
202208

203-
<span class="caption">Listing 20-4: Using the safe `split_at_mut`
204-
function</span>
209+
</Listing>
205210

206211
We can’t implement this function using only safe Rust. An attempt might look
207212
something like Listing 20-5, which won’t compile. For simplicity, we’ll
208213
implement `split_at_mut` as a function rather than a method and only for slices
209214
of `i32` values rather than for a generic type `T`.
210215

216+
<Listing number="20-5" caption="An attempted implementation of `split_at_mut` using only safe Rust">
217+
211218
```rust,ignore,does_not_compile
212219
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-05/src/main.rs:here}}
213220
```
214221

215-
<span class="caption">Listing 20-5: An attempted implementation of
216-
`split_at_mut` using only safe Rust</span>
222+
</Listing>
217223

218224
This function first gets the total length of the slice. Then it asserts that
219225
the index given as a parameter is within the slice by checking whether it’s
@@ -240,12 +246,13 @@ know code is okay, but Rust doesn’t, it’s time to reach for unsafe code.
240246
Listing 20-6 shows how to use an `unsafe` block, a raw pointer, and some calls
241247
to unsafe functions to make the implementation of `split_at_mut` work.
242248

249+
<Listing number="20-6" caption="Using unsafe code in the implementation of the `split_at_mut` function">
250+
243251
```rust
244252
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-06/src/main.rs:here}}
245253
```
246254

247-
<span class="caption">Listing 20-6: Using unsafe code in the implementation of
248-
the `split_at_mut` function</span>
255+
</Listing>
249256

250257
Recall from [“The Slice Type”][the-slice-type]<!-- ignore --> section in
251258
Chapter 4 that slices are a pointer to some data and the length of the slice.
@@ -282,12 +289,13 @@ In contrast, the use of `slice::from_raw_parts_mut` in Listing 20-7 would
282289
likely crash when the slice is used. This code takes an arbitrary memory
283290
location and creates a slice 10,000 items long.
284291

292+
<Listing number="20-7" caption="Creating a slice from an arbitrary memory location">
293+
285294
```rust
286295
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-07/src/main.rs:here}}
287296
```
288297

289-
<span class="caption">Listing 20-7: Creating a slice from an arbitrary memory
290-
location</span>
298+
</Listing>
291299

292300
We don’t own the memory at this arbitrary location, and there is no guarantee
293301
that the slice this code creates contains valid `i32` values. Attempting to use
@@ -307,14 +315,13 @@ always unsafe to call from Rust code. The reason is that other languages don’t
307315
enforce Rust’s rules and guarantees, and Rust can’t check them, so
308316
responsibility falls on the programmer to ensure safety.
309317

310-
<span class="filename">Filename: src/main.rs</span>
318+
<Listing number="20-8" file-name="src/main.rs" caption="Declaring and calling an `extern` function defined in another language">
311319

312320
```rust
313321
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-08/src/main.rs}}
314322
```
315323

316-
<span class="caption">Listing 20-8: Declaring and calling an `extern` function
317-
defined in another language</span>
324+
</Listing>
318325

319326
Within the `extern "C"` block, we list the names and signatures of external
320327
functions from another language we want to call. The `"C"` part defines which
@@ -357,14 +364,13 @@ In Rust, global variables are called *static* variables. Listing 20-9 shows an
357364
example declaration and use of a static variable with a string slice as a
358365
value.
359366
360-
<span class="filename">Filename: src/main.rs</span>
367+
<Listing number="20-9" file-name="src/main.rs" caption="Defining and using an immutable static variable">
361368
362369
```rust
363370
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-09/src/main.rs}}
364371
```
365372
366-
<span class="caption">Listing 20-9: Defining and using an immutable static
367-
variable</span>
373+
</Listing>
368374
369375
Static variables are similar to constants, which we discussed in the
370376
[“Differences Between Variables and
@@ -383,14 +389,13 @@ variables can be mutable. Accessing and modifying mutable static variables is
383389
*unsafe*. Listing 20-10 shows how to declare, access, and modify a mutable
384390
static variable named `COUNTER`.
385391
386-
<span class="filename">Filename: src/main.rs</span>
392+
<Listing number="20-10" file-name="src/main.rs" caption="Reading from or writing to a mutable static variable is unsafe">
387393
388394
```rust
389395
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-10/src/main.rs}}
390396
```
391397
392-
<span class="caption">Listing 20-10: Reading from or writing to a mutable
393-
static variable is unsafe</span>
398+
</Listing>
394399
395400
As with regular variables, we specify mutability using the `mut` keyword. Any
396401
code that reads or writes from `COUNTER` must be within an `unsafe` block. This
@@ -412,12 +417,13 @@ declare that a trait is `unsafe` by adding the `unsafe` keyword before `trait`
412417
and marking the implementation of the trait as `unsafe` too, as shown in
413418
Listing 20-11.
414419
420+
<Listing number="20-11" caption="Defining and implementing an unsafe trait">
421+
415422
```rust
416423
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-11/src/main.rs}}
417424
```
418425
419-
<span class="caption">Listing 20-11: Defining and implementing an unsafe
420-
trait</span>
426+
</Listing>
421427
422428
By using `unsafe impl`, we’re promising that we’ll uphold the invariants that
423429
the compiler can’t verify.

0 commit comments

Comments
 (0)