Skip to content

Commit 206bb69

Browse files
authored
Merge pull request #3980 from SpectralPixel/listing-preprocessor-chapter-09
Convert Listings in Chapter 09 to `<Listing>`
2 parents ec6b1b4 + c589649 commit 206bb69

3 files changed

+40
-40
lines changed

src/ch09-01-unrecoverable-errors-with-panic.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ panic occurs to make it easier to track down the source of the panic.
3131
3232
Let’s try calling `panic!` in a simple program:
3333
34-
<span class="filename">Filename: src/main.rs</span>
34+
<Listing file-name="src/main.rs">
3535
3636
```rust,should_panic,panics
3737
{{#rustdoc_include ../listings/ch09-error-handling/no-listing-01-panic/src/main.rs}}
3838
```
3939
40+
</Listing>
41+
4042
When you run the program, you’ll see something like this:
4143

4244
```console
@@ -64,14 +66,13 @@ a `panic!` call comes from a library because of a bug in our code instead of
6466
from our code calling the macro directly. Listing 9-1 has some code that
6567
attempts to access an index in a vector beyond the range of valid indexes.
6668

67-
<span class="filename">Filename: src/main.rs</span>
69+
<Listing number="9-1" file-name="src/main.rs" caption="Attempting to access an element beyond the end of a vector, which will cause a call to `panic!`">
6870

6971
```rust,should_panic,panics
7072
{{#rustdoc_include ../listings/ch09-error-handling/listing-09-01/src/main.rs}}
7173
```
7274

73-
<span class="caption">Listing 9-1: Attempting to access an element beyond the
74-
end of a vector, which will cause a call to `panic!`</span>
75+
</Listing>
7576

7677
Here, we’re attempting to access the 100th element of our vector (which is at
7778
index 99 because indexing starts at zero), but the vector has only three
@@ -117,6 +118,8 @@ copy the backtrace output below
117118
check the backtrace number mentioned in the text below the listing
118119
-->
119120

121+
<Listing number="9-2" caption="The backtrace generated by a call to `panic!` displayed when the environment variable `RUST_BACKTRACE` is set">
122+
120123
```console
121124
$ RUST_BACKTRACE=1 cargo run
122125
thread 'main' panicked at src/main.rs:4:6:
@@ -141,8 +144,7 @@ stack backtrace:
141144
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
142145
```
143146

144-
<span class="caption">Listing 9-2: The backtrace generated by a call to
145-
`panic!` displayed when the environment variable `RUST_BACKTRACE` is set</span>
147+
</Listing>
146148

147149
That’s a lot of output! The exact output you see might be different depending
148150
on your operating system and Rust version. In order to get backtraces with this

src/ch09-02-recoverable-errors-with-result.md

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ return may differ.
2929
Let’s call a function that returns a `Result` value because the function could
3030
fail. In Listing 9-3 we try to open a file.
3131

32-
<span class="filename">Filename: src/main.rs</span>
32+
<Listing number="9-3" file-name="src/main.rs" caption="Opening a file">
3333

3434
```rust
3535
{{#rustdoc_include ../listings/ch09-error-handling/listing-09-03/src/main.rs}}
3636
```
3737

38-
<span class="caption">Listing 9-3: Opening a file</span>
38+
</Listing>
3939

4040
The return type of `File::open` is a `Result<T, E>`. The generic parameter `T`
4141
has been filled in by the implementation of `File::open` with the type of the
@@ -59,14 +59,13 @@ on the value `File::open` returns. Listing 9-4 shows one way to handle the
5959
`Result` using a basic tool, the `match` expression that we discussed in
6060
Chapter 6.
6161

62-
<span class="filename">Filename: src/main.rs</span>
62+
<Listing number="9-4" file-name="src/main.rs" caption="Using a `match` expression to handle the `Result` variants that might be returned">
6363

6464
```rust,should_panic
6565
{{#rustdoc_include ../listings/ch09-error-handling/listing-09-04/src/main.rs}}
6666
```
6767

68-
<span class="caption">Listing 9-4: Using a `match` expression to handle the
69-
`Result` variants that might be returned</span>
68+
</Listing>
7069

7170
Note that, like the `Option` enum, the `Result` enum and its variants have been
7271
brought into scope by the prelude, so we don’t need to specify `Result::`
@@ -98,7 +97,7 @@ reason—for example, because we didn’t have permission to open the file—we
9897
want the code to `panic!` in the same way it did in Listing 9-4. For this, we
9998
add an inner `match` expression, shown in Listing 9-5.
10099

101-
<span class="filename">Filename: src/main.rs</span>
100+
<Listing number="9-5" file-name="src/main.rs" caption="Handling different kinds of errors in different ways">
102101

103102
<!-- ignore this test because otherwise it creates hello.txt which causes other
104103
tests to fail lol -->
@@ -107,8 +106,7 @@ tests to fail lol -->
107106
{{#rustdoc_include ../listings/ch09-error-handling/listing-09-05/src/main.rs}}
108107
```
109108

110-
<span class="caption">Listing 9-5: Handling different kinds of errors in
111-
different ways</span>
109+
</Listing>
112110

113111
The type of the value that `File::open` returns inside the `Err` variant is
114112
`io::Error`, which is a struct provided by the standard library. This struct
@@ -172,12 +170,14 @@ Listing 9-4. If the `Result` value is the `Ok` variant, `unwrap` will return
172170
the value inside the `Ok`. If the `Result` is the `Err` variant, `unwrap` will
173171
call the `panic!` macro for us. Here is an example of `unwrap` in action:
174172
175-
<span class="filename">Filename: src/main.rs</span>
173+
<Listing file-name="src/main.rs">
176174
177175
```rust,should_panic
178176
{{#rustdoc_include ../listings/ch09-error-handling/no-listing-04-unwrap/src/main.rs}}
179177
```
180178
179+
</Listing>
180+
181181
If we run this code without a *hello.txt* file, we’ll see an error message from
182182
the `panic!` call that the `unwrap` method makes:
183183

@@ -197,12 +197,14 @@ Using `expect` instead of `unwrap` and providing good error messages can convey
197197
your intent and make tracking down the source of a panic easier. The syntax of
198198
`expect` looks like this:
199199

200-
<span class="filename">Filename: src/main.rs</span>
200+
<Listing file-name="src/main.rs">
201201

202202
```rust,should_panic
203203
{{#rustdoc_include ../listings/ch09-error-handling/no-listing-05-expect/src/main.rs}}
204204
```
205205

206+
</Listing>
207+
206208
We use `expect` in the same way as `unwrap`: to return the file handle or call
207209
the `panic!` macro. The error message used by `expect` in its call to `panic!`
208210
will be the parameter that we pass to `expect`, rather than the default
@@ -237,7 +239,7 @@ For example, Listing 9-6 shows a function that reads a username from a file. If
237239
the file doesn’t exist or can’t be read, this function will return those errors
238240
to the code that called the function.
239241

240-
<span class="filename">Filename: src/main.rs</span>
242+
<Listing number="9-6" file-name="src/main.rs" caption="A function that returns errors to the calling code using `match`">
241243

242244
<!-- Deliberately not using rustdoc_include here; the `main` function in the
243245
file panics. We do want to include it for reader experimentation purposes, but
@@ -247,8 +249,7 @@ don't want to include it for rustdoc testing purposes. -->
247249
{{#include ../listings/ch09-error-handling/listing-09-06/src/main.rs:here}}
248250
```
249251

250-
<span class="caption">Listing 9-6: A function that returns errors to the
251-
calling code using `match`</span>
252+
</Listing>
252253

253254
This function can be written in a much shorter way, but we’re going to start by
254255
doing a lot of it manually in order to explore error handling; at the end,
@@ -307,7 +308,7 @@ Listing 9-7 shows an implementation of `read_username_from_file` that has the
307308
same functionality as in Listing 9-6, but this implementation uses the `?`
308309
operator.
309310

310-
<span class="filename">Filename: src/main.rs</span>
311+
<Listing number="9-7" file-name="src/main.rs" caption="A function that returns errors to the calling code using the `?` operator">
311312

312313
<!-- Deliberately not using rustdoc_include here; the `main` function in the
313314
file panics. We do want to include it for reader experimentation purposes, but
@@ -317,8 +318,7 @@ don't want to include it for rustdoc testing purposes. -->
317318
{{#include ../listings/ch09-error-handling/listing-09-07/src/main.rs:here}}
318319
```
319320

320-
<span class="caption">Listing 9-7: A function that returns errors to the
321-
calling code using the `?` operator</span>
321+
</Listing>
322322

323323
The `?` placed after a `Result` value is defined to work in almost the same way
324324
as the `match` expressions we defined to handle the `Result` values in Listing
@@ -355,7 +355,7 @@ The `?` operator eliminates a lot of boilerplate and makes this function’s
355355
implementation simpler. We could even shorten this code further by chaining
356356
method calls immediately after the `?`, as shown in Listing 9-8.
357357

358-
<span class="filename">Filename: src/main.rs</span>
358+
<Listing number="9-8" file-name="src/main.rs" caption="Chaining method calls after the `?` operator">
359359

360360
<!-- Deliberately not using rustdoc_include here; the `main` function in the
361361
file panics. We do want to include it for reader experimentation purposes, but
@@ -365,8 +365,7 @@ don't want to include it for rustdoc testing purposes. -->
365365
{{#include ../listings/ch09-error-handling/listing-09-08/src/main.rs:here}}
366366
```
367367

368-
<span class="caption">Listing 9-8: Chaining method calls after the `?`
369-
operator</span>
368+
</Listing>
370369

371370
We’ve moved the creation of the new `String` in `username` to the beginning of
372371
the function; that part hasn’t changed. Instead of creating a variable
@@ -379,7 +378,7 @@ this is just a different, more ergonomic way to write it.
379378

380379
Listing 9-9 shows a way to make this even shorter using `fs::read_to_string`.
381380

382-
<span class="filename">Filename: src/main.rs</span>
381+
<Listing number="9-9" file-name="src/main.rs" caption="Using `fs::read_to_string` instead of opening and then reading the file">
383382

384383
<!-- Deliberately not using rustdoc_include here; the `main` function in the
385384
file panics. We do want to include it for reader experimentation purposes, but
@@ -389,8 +388,7 @@ don't want to include it for rustdoc testing purposes. -->
389388
{{#include ../listings/ch09-error-handling/listing-09-09/src/main.rs:here}}
390389
```
391390

392-
<span class="caption">Listing 9-9: Using `fs::read_to_string` instead of
393-
opening and then reading the file</span>
391+
</Listing>
394392

395393
Reading a file into a string is a fairly common operation, so the standard
396394
library provides the convenient `fs::read_to_string` function that opens the
@@ -413,14 +411,13 @@ In Listing 9-10, let’s look at the error we’ll get if we use the `?` operato
413411
in a `main` function with a return type that is incompatible with the type of
414412
the value we use `?` on.
415413

416-
<span class="filename">Filename: src/main.rs</span>
414+
<Listing number="9-10" file-name="src/main.rs" caption="Attempting to use the `?` in the `main` function that returns `()` won’t compile.">
417415

418416
```rust,ignore,does_not_compile
419417
{{#rustdoc_include ../listings/ch09-error-handling/listing-09-10/src/main.rs}}
420418
```
421419

422-
<span class="caption">Listing 9-10: Attempting to use the `?` in the `main`
423-
function that returns `()` won’t compile.</span>
420+
</Listing>
424421

425422
This code opens a file, which might fail. The `?` operator follows the `Result`
426423
value returned by `File::open`, but this `main` function has the return type of
@@ -451,12 +448,13 @@ resultant value of the expression, and the function continues. Listing 9-11 has
451448
an example of a function that finds the last character of the first line in the
452449
given text.
453450

451+
<Listing number="9-11" caption="Using the `?` operator on an `Option<T>` value">
452+
454453
```rust
455454
{{#rustdoc_include ../listings/ch09-error-handling/listing-09-11/src/main.rs:here}}
456455
```
457456

458-
<span class="caption">Listing 9-11: Using the `?` operator on an `Option<T>`
459-
value</span>
457+
</Listing>
460458

461459
This function returns `Option<char>` because it’s possible that there is a
462460
character there, but it’s also possible that there isn’t. This code takes the
@@ -496,14 +494,13 @@ from Listing 9-10, but we’ve changed the return type of `main` to be
496494
`Result<(), Box<dyn Error>>` and added a return value `Ok(())` to the end. This
497495
code will now compile.
498496

499-
<span class="filename">Filename: src/main.rs</span>
497+
<Listing number="9-12" file-name="src/main.rs" caption="Changing `main` to return `Result<(), E>` allows the use of the `?` operator on `Result` values.">
500498

501499
```rust,ignore
502500
{{#rustdoc_include ../listings/ch09-error-handling/listing-09-12/src/main.rs}}
503501
```
504502

505-
<span class="caption">Listing 9-12: Changing `main` to return `Result<(), E>`
506-
allows the use of the `?` operator on `Result` values.</span>
503+
</Listing>
507504

508505
The `Box<dyn Error>` type is a *trait object*, which we’ll talk about in the
509506
[“Using Trait Objects that Allow for Values of Different

src/ch09-03-to-panic-or-not-to-panic.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,14 @@ One way to do this would be to parse the guess as an `i32` instead of only a
140140
`u32` to allow potentially negative numbers, and then add a check for the
141141
number being in range, like so:
142142

143-
<span class="filename">Filename: src/main.rs</span>
143+
<Listing file-name="src/main.rs">
144144

145145
```rust,ignore
146146
{{#rustdoc_include ../listings/ch09-error-handling/no-listing-09-guess-out-of-range/src/main.rs:here}}
147147
```
148148

149+
</Listing>
150+
149151
The `if` expression checks whether our value is out of range, tells the user
150152
about the problem, and calls `continue` to start the next iteration of the loop
151153
and ask for another guess. After the `if` expression, we can proceed with the
@@ -164,14 +166,13 @@ confidently use the values they receive. Listing 9-13 shows one way to define a
164166
`Guess` type that will only create an instance of `Guess` if the `new` function
165167
receives a value between 1 and 100.
166168

167-
<span class="filename">Filename: src/lib.rs</span>
169+
<Listing number="9-13" caption="A `Guess` type that will only continue with values between 1 and 100">
168170

169171
```rust
170172
{{#rustdoc_include ../listings/ch09-error-handling/listing-09-13/src/lib.rs}}
171173
```
172174

173-
<span class="caption">Listing 9-13: A `Guess` type that will only continue with
174-
values between 1 and 100</span>
175+
</Listing>
175176

176177
First we define a struct named `Guess` that has a field named `value` that
177178
holds an `i32`. This is where the number will be stored.

0 commit comments

Comments
 (0)