@@ -91,11 +91,13 @@ interface with another language or hardware where Rust’s guarantees don’t ap
91
91
Listing 20-1 shows how to create an immutable and a mutable raw pointer from
92
92
references.
93
93
94
+ <Listing number =" 20-1 " caption =" Creating raw pointers from references " >
95
+
94
96
``` rust
95
97
{{#rustdoc_include .. / listings / ch20 - advanced - features / listing - 20 - 01 / src / main . rs: here }}
96
98
```
97
99
98
- <span class = " caption " >Listing 20-1: Creating raw pointers from references</ span >
100
+ </ Listing >
99
101
100
102
Notice that we don’t include the ` unsafe ` keyword in this code. We can create
101
103
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
115
117
fault. Usually, there is no good reason to write code like this, but it is
116
118
possible.
117
119
120
+ <Listing number =" 20-2 " caption =" Creating a raw pointer to an arbitrary memory address " >
121
+
118
122
``` rust
119
123
{{#rustdoc_include .. / listings / ch20 - advanced - features / listing - 20 - 02 / src / main . rs: here }}
120
124
```
121
125
122
- <span class =" caption " >Listing 20-2: Creating a raw pointer to an arbitrary
123
- memory address</span >
126
+ </Listing >
124
127
125
128
Recall that we can create raw pointers in safe code, but we can’t * dereference*
126
129
raw pointers and read the data being pointed to. In Listing 20-3, we use the
127
130
dereference operator ` * ` on a raw pointer that requires an ` unsafe ` block.
128
131
132
+ <Listing number =" 20-3 " caption =" Dereferencing raw pointers within an `unsafe` block " >
133
+
129
134
``` rust
130
135
{{#rustdoc_include .. / listings / ch20 - advanced - features / listing - 20 - 03 / src / main . rs: here }}
131
136
```
132
137
133
- <span class =" caption " >Listing 20-3: Dereferencing raw pointers within an
134
- ` unsafe ` block</span >
138
+ </Listing >
135
139
136
140
Creating a pointer does no harm; it’s only when we try to access the value that
137
141
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
196
200
one slice and makes it two by splitting the slice at the index given as an
197
201
argument. Listing 20-4 shows how to use ` split_at_mut ` .
198
202
203
+ <Listing number =" 20-4 " caption =" Using the safe `split_at_mut` function " >
204
+
199
205
``` rust
200
206
{{#rustdoc_include .. / listings / ch20 - advanced - features / listing - 20 - 04 / src / main . rs: here }}
201
207
```
202
208
203
- <span class =" caption " >Listing 20-4: Using the safe ` split_at_mut `
204
- function</span >
209
+ </Listing >
205
210
206
211
We can’t implement this function using only safe Rust. An attempt might look
207
212
something like Listing 20-5, which won’t compile. For simplicity, we’ll
208
213
implement ` split_at_mut ` as a function rather than a method and only for slices
209
214
of ` i32 ` values rather than for a generic type ` T ` .
210
215
216
+ <Listing number =" 20-5 " caption =" An attempted implementation of `split_at_mut` using only safe Rust " >
217
+
211
218
``` rust,ignore,does_not_compile
212
219
{{#rustdoc_include ../listings/ch20-advanced-features/listing-20-05/src/main.rs:here}}
213
220
```
214
221
215
- <span class =" caption " >Listing 20-5: An attempted implementation of
216
- ` split_at_mut ` using only safe Rust</span >
222
+ </Listing >
217
223
218
224
This function first gets the total length of the slice. Then it asserts that
219
225
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.
240
246
Listing 20-6 shows how to use an ` unsafe ` block, a raw pointer, and some calls
241
247
to unsafe functions to make the implementation of ` split_at_mut ` work.
242
248
249
+ <Listing number =" 20-6 " caption =" Using unsafe code in the implementation of the `split_at_mut` function " >
250
+
243
251
``` rust
244
252
{{#rustdoc_include .. / listings / ch20 - advanced - features / listing - 20 - 06 / src / main . rs: here }}
245
253
```
246
254
247
- <span class =" caption " >Listing 20-6: Using unsafe code in the implementation of
248
- the ` split_at_mut ` function</span >
255
+ </Listing >
249
256
250
257
Recall from [ “The Slice Type”] [ the-slice-type ] <!-- ignore --> section in
251
258
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
282
289
likely crash when the slice is used. This code takes an arbitrary memory
283
290
location and creates a slice 10,000 items long.
284
291
292
+ <Listing number =" 20-7 " caption =" Creating a slice from an arbitrary memory location " >
293
+
285
294
``` rust
286
295
{{#rustdoc_include .. / listings / ch20 - advanced - features / listing - 20 - 07 / src / main . rs: here }}
287
296
```
288
297
289
- <span class =" caption " >Listing 20-7: Creating a slice from an arbitrary memory
290
- location</span >
298
+ </Listing >
291
299
292
300
We don’t own the memory at this arbitrary location, and there is no guarantee
293
301
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
307
315
enforce Rust’s rules and guarantees, and Rust can’t check them, so
308
316
responsibility falls on the programmer to ensure safety.
309
317
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 " >
311
319
312
320
``` rust
313
321
{{#rustdoc_include .. / listings / ch20 - advanced - features / listing - 20 - 08 / src / main . rs}}
314
322
```
315
323
316
- <span class =" caption " >Listing 20-8: Declaring and calling an ` extern ` function
317
- defined in another language</span >
324
+ </Listing >
318
325
319
326
Within the ` extern "C" ` block, we list the names and signatures of external
320
327
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
357
364
example declaration and use of a static variable with a string slice as a
358
365
value.
359
366
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" >
361
368
362
369
```rust
363
370
{{#rustdoc_include .. / listings/ ch20- advanced- features/ listing- 20- 09/ src/ main. rs}}
364
371
```
365
372
366
- <span class= "caption">Listing 20- 9: Defining and using an immutable static
367
- variable</ span>
373
+ </ Listing >
368
374
369
375
Static variables are similar to constants, which we discussed in the
370
376
[“Differences Between Variables and
@@ -383,14 +389,13 @@ variables can be mutable. Accessing and modifying mutable static variables is
383
389
* unsafe * . Listing 20- 10 shows how to declare, access, and modify a mutable
384
390
static variable named `COUNTER `.
385
391
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 " >
387
393
388
394
```rust
389
395
{{#rustdoc_include .. / listings/ ch20- advanced- features/ listing- 20- 10/ src/ main. rs}}
390
396
```
391
397
392
- <span class= "caption">Listing 20- 10: Reading from or writing to a mutable
393
- static variable is unsafe </ span>
398
+ </ Listing >
394
399
395
400
As with regular variables, we specify mutability using the `mut ` keyword. Any
396
401
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`
412
417
and marking the implementation of the trait as `unsafe ` too, as shown in
413
418
Listing 20- 11.
414
419
420
+ <Listing number= "20- 11" caption= "Defining and implementing an unsafe trait ">
421
+
415
422
```rust
416
423
{{#rustdoc_include .. / listings/ ch20- advanced- features/ listing- 20- 11/ src/ main. rs}}
417
424
```
418
425
419
- <span class= "caption">Listing 20- 11: Defining and implementing an unsafe
420
- trait </ span>
426
+ </ Listing >
421
427
422
428
By using `unsafe impl `, we’re promising that we’ll uphold the invariants that
423
429
the compiler can’t verify.
0 commit comments