Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert Listings in Chapter 10 to <Listing> #3978

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
89cb629
Convert Listing 10-1 to `<Listing>`
SpectralPixel Jun 19, 2024
c04c59b
Convert Listing 10-2 to `<Listing>`
SpectralPixel Jun 19, 2024
d6c8701
Convert Listing 10-3 to `<Listing>`
SpectralPixel Jun 19, 2024
0e1f400
Fixed bugs in `<Listing>`s 10-1 to 10-3 (inclusive)
SpectralPixel Jun 19, 2024
fc74310
Convert Listing 10-4 to `<Listing>`
SpectralPixel Jun 19, 2024
0e7e50b
Convert Listing 10-5 to `<Listing>`
SpectralPixel Jun 19, 2024
07efb8f
Convert Listing 10-6 to `<Listing>`
SpectralPixel Jun 19, 2024
1a4046a
Convert Listing 10-7 to `<Listing>`
SpectralPixel Jun 19, 2024
9de7f2d
Convert Listing 10-8 to `<Listing>`
SpectralPixel Jun 19, 2024
e005c67
Convert Listing 10-9 to `<Listing>`
SpectralPixel Jun 19, 2024
fd55a3b
Convert Listing 10-10 to `<Listing>`
SpectralPixel Jun 19, 2024
3a8203e
Convert Listing 10-10 to `<Listing>`
SpectralPixel Jun 19, 2024
0bc7e4a
Convert Listing 10-12 to `<Listing>`
SpectralPixel Jun 19, 2024
c550845
Convert Listing 10-13 to `<Listing>`
SpectralPixel Jun 19, 2024
6dff902
Convert Listing 10-14 to `<Listing>`
SpectralPixel Jun 19, 2024
cb2d469
Convert Listing 10-15 to `<Listing>`
SpectralPixel Jun 19, 2024
ed9eea7
Convert Listing 10-16 to `<Listing>`
SpectralPixel Jun 24, 2024
ef4e451
Convert Listing 10-17 to `<Listing>`
SpectralPixel Jun 26, 2024
bdc62e9
Convert Listing 10-18 to `<Listing>`
SpectralPixel Jun 26, 2024
eb3423b
Convert Listing 10-19 to `<Listing>`
SpectralPixel Jun 26, 2024
b1cfe63
Convert Listing 10-20 to `<Listing>`
SpectralPixel Jun 26, 2024
7d9e87f
Convert Listing 10-21 to `<Listing>`
SpectralPixel Jun 26, 2024
b91f208
Convert Listing 10-22 to `<Listing>`
SpectralPixel Jun 26, 2024
704e4dd
Convert Listing 10-23 to `<Listing>`
SpectralPixel Jun 26, 2024
d4d4a1d
Convert Listing 10-24 to `<Listing>`
SpectralPixel Jun 26, 2024
36fb0ae
Converted unnamed listing to `<Listing>` (Chapter 10.3, 1/2)
SpectralPixel Jun 26, 2024
b8c7c99
Converted unnamed listing to `<Listing>` (Chapter 10.3, 2/2)
SpectralPixel Jun 26, 2024
9151143
Convert Listing 10-25 to `<Listing>`
SpectralPixel Jun 26, 2024
e82cd3f
Chapter 10 - Wrap all `<Listing>`s to comply with the virtual 80 char…
SpectralPixel Jul 17, 2024
cddaed6
Convert unnamed `<Listing>` (Chapter 10.1)
SpectralPixel Jul 17, 2024
8b443bc
Back out "Chapter 10 - Wrap all `<Listing>`s to comply with the virtu…
chriskrycho Oct 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/ch10-00-generics.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ duplicated code that can use generics.
We’ll begin with the short program in Listing 10-1 that finds the largest
number in a list.

<span class="filename">Filename: src/main.rs</span>
<Listing number="10-1" file-name="src/main.rs" caption="Finding the largest number in a list of numbers">

```rust
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-01/src/main.rs:here}}
```

<span class="caption">Listing 10-1: Finding the largest number in a list of
numbers</span>
</Listing>

We store a list of integers in the variable `number_list` and place a reference
to the first number in the list in a variable named `largest`. We then iterate
Expand All @@ -64,14 +63,13 @@ We’ve now been tasked with finding the largest number in two different lists o
numbers. To do so, we can choose to duplicate the code in Listing 10-1 and use
the same logic at two different places in the program, as shown in Listing 10-2.

<span class="filename">Filename: src/main.rs</span>
<Listing number="10-2" file-name="src/main.rs" caption="Code to find the largest number in *two* lists of numbers">

```rust
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-02/src/main.rs}}
```

<span class="caption">Listing 10-2: Code to find the largest number in *two*
lists of numbers</span>
</Listing>

Although this code works, duplicating code is tedious and error prone. We also
have to remember to update the code in multiple places when we want to change
Expand All @@ -87,14 +85,13 @@ function named `largest`. Then we call the function to find the largest number
in the two lists from Listing 10-2. We could also use the function on any other
list of `i32` values we might have in the future.

<span class="filename">Filename: src/main.rs</span>
<Listing number="10-3" file-name="src/main.rs" caption="Abstracted code to find the largest number in two lists">

```rust
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-03/src/main.rs:here}}
```

<span class="caption">Listing 10-3: Abstracted code to find the largest number
in two lists</span>
</Listing>

The `largest` function has a parameter called `list`, which represents any
concrete slice of `i32` values we might pass into the function. As a result,
Expand Down
45 changes: 19 additions & 26 deletions src/ch10-01-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ Continuing with our `largest` function, Listing 10-4 shows two functions that
both find the largest value in a slice. We’ll then combine these into a single
function that uses generics.

<span class="filename">Filename: src/main.rs</span>
<Listing number="10-4" file-name="src/main.rs" caption="Two functions that differ only in their names and in the types in their signatures">

```rust
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-04/src/main.rs:here}}
```

<span class="caption">Listing 10-4: Two functions that differ only in their
names and in the types in their signatures</span>
</Listing>

The `largest_i32` function is the one we extracted in Listing 10-3 that finds
the largest `i32` in a slice. The `largest_char` function finds the largest
Expand Down Expand Up @@ -58,14 +57,13 @@ data type in its signature. The listing also shows how we can call the function
with either a slice of `i32` values or `char` values. Note that this code won’t
compile yet, but we’ll fix it later in this chapter.

<span class="filename">Filename: src/main.rs</span>
<Listing number="10-5" file-name="src/main.rs" caption="The `largest` function using generic type parameters; this doesn’t compile yet">

```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/src/main.rs}}
```

<span class="caption">Listing 10-5: The `largest` function using generic type
parameters; this doesn’t compile yet</span>
</Listing>

If we compile this code right now, we’ll get this error:

Expand All @@ -90,14 +88,13 @@ We can also define structs to use a generic type parameter in one or more
fields using the `<>` syntax. Listing 10-6 defines a `Point<T>` struct to hold
`x` and `y` coordinate values of any type.

<span class="filename">Filename: src/main.rs</span>
<Listing number="10-6" file-name="src/main.rs" caption="A `Point<T>` struct that holds `x` and `y` values of type `T`">

```rust
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-06/src/main.rs}}
```

<span class="caption">Listing 10-6: A `Point<T>` struct that holds `x` and `y`
values of type `T`</span>
</Listing>

The syntax for using generics in struct definitions is similar to that used in
function definitions. First we declare the name of the type parameter inside
Expand All @@ -111,14 +108,13 @@ the fields `x` and `y` are *both* that same type, whatever that type may be. If
we create an instance of a `Point<T>` that has values of different types, as in
Listing 10-7, our code won’t compile.

<span class="filename">Filename: src/main.rs</span>
<Listing number="10-7" file-name="src/main.rs" caption="The fields `x` and `y` must be the same type because both have the same generic data type `T`.">

```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/src/main.rs}}
```

<span class="caption">Listing 10-7: The fields `x` and `y` must be the same
type because both have the same generic data type `T`.</span>
</Listing>

In this example, when we assign the integer value `5` to `x`, we let the
compiler know that the generic type `T` will be an integer for this instance of
Expand All @@ -134,14 +130,13 @@ different types, we can use multiple generic type parameters. For example, in
Listing 10-8, we change the definition of `Point` to be generic over types `T`
and `U` where `x` is of type `T` and `y` is of type `U`.

<span class="filename">Filename: src/main.rs</span>
<Listing number="10-8" file-name="src/main.rs" caption="A `Point<T, U>` generic over two types so that `x` and `y` can be values of different types">

```rust
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-08/src/main.rs}}
```

<span class="caption">Listing 10-8: A `Point<T, U>` generic over two types so
that `x` and `y` can be values of different types</span>
</Listing>

Now all the instances of `Point` shown are allowed! You can use as many generic
type parameters in a definition as you want, but using more than a few makes
Expand Down Expand Up @@ -198,15 +193,13 @@ We can implement methods on structs and enums (as we did in Chapter 5) and use
generic types in their definitions too. Listing 10-9 shows the `Point<T>`
struct we defined in Listing 10-6 with a method named `x` implemented on it.

<span class="filename">Filename: src/main.rs</span>
<Listing number="10-9" file-name="src/main.rs" caption="Implementing a method named `x` on the `Point<T>` struct that will return a reference to the `x` field of type `T`">

```rust
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-09/src/main.rs}}
```

<span class="caption">Listing 10-9: Implementing a method named `x` on the
`Point<T>` struct that will return a reference to the `x` field of type
`T`</span>
</Listing>

Here, we’ve defined a method named `x` on `Point<T>` that returns a reference
to the data in the field `x`.
Expand All @@ -226,14 +219,13 @@ type. We could, for example, implement methods only on `Point<f32>` instances
rather than on `Point<T>` instances with any generic type. In Listing 10-10 we
use the concrete type `f32`, meaning we don’t declare any types after `impl`.

<span class="filename">Filename: src/main.rs</span>
<Listing number="10-10" file-name="src/main.rs" caption="An `impl` block that only applies to a struct with a particular concrete type for the generic type parameter `T`">

```rust
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-10/src/main.rs:here}}
```

<span class="caption">Listing 10-10: An `impl` block that only applies to a
struct with a particular concrete type for the generic type parameter `T`</span>
</Listing>

This code means the type `Point<f32>` will have a `distance_from_origin`
method; other instances of `Point<T>` where `T` is not of type `f32` will not
Expand All @@ -248,14 +240,13 @@ signature to make the example clearer. The method creates a new `Point`
instance with the `x` value from the `self` `Point` (of type `X1`) and the `y`
value from the passed-in `Point` (of type `Y2`).

<span class="filename">Filename: src/main.rs</span>
<Listing number="10-11" file-name="src/main.rs" caption="A method that uses generic types different from its struct’s definition">

```rust
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/src/main.rs}}
```

<span class="caption">Listing 10-11: A method that uses generic types different
from its struct’s definition</span>
</Listing>

In `main`, we’ve defined a `Point` that has an `i32` for `x` (with value `5`)
and an `f64` for `y` (with value `10.4`). The `p2` variable is a `Point` struct
Expand Down Expand Up @@ -304,7 +295,7 @@ definition with the specific ones.
The monomorphized version of the code looks similar to the following (the
compiler uses different names than what we’re using here for illustration):

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

```rust
enum Option_i32 {
Expand All @@ -323,6 +314,8 @@ fn main() {
}
```

</Listing>

The generic `Option<T>` is replaced with the specific definitions created by
the compiler. Because Rust compiles generic code into code that specifies the
type in each instance, we pay no runtime cost for using generics. When the code
Expand Down
20 changes: 8 additions & 12 deletions src/ch10-02-traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ instance. To do this, we need a summary from each type, and we’ll request that
summary by calling a `summarize` method on an instance. Listing 10-12 shows the
definition of a public `Summary` trait that expresses this behavior.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="10-12" file-name="src/lib.rs" caption="A `Summary` trait that consists of the behavior provided by a `summarize` method">

```rust,noplayground
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-12/src/lib.rs}}
```

<span class="caption">Listing 10-12: A `Summary` trait that consists of the
behavior provided by a `summarize` method</span>
</Listing>

Here, we declare a trait using the `trait` keyword and then the trait’s name,
which is `Summary` in this case. We also declare the trait as `pub` so that
Expand Down Expand Up @@ -62,14 +61,13 @@ the headline, the author, and the location to create the return value of
followed by the entire text of the tweet, assuming that the tweet content is
already limited to 280 characters.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="10-13" file-name="src/lib.rs" caption="Implementing the `Summary` trait on the `NewsArticle` and `Tweet` types">

```rust,noplayground
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-13/src/lib.rs:here}}
```

<span class="caption">Listing 10-13: Implementing the `Summary` trait on the
`NewsArticle` and `Tweet` types</span>
</Listing>

Implementing a trait on a type is similar to implementing regular methods. The
difference is that after `impl`, we put the trait name we want to implement,
Expand Down Expand Up @@ -124,14 +122,13 @@ In Listing 10-14, we specify a default string for the `summarize` method of the
`Summary` trait instead of only defining the method signature, as we did in
Listing 10-12.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="10-14" file-name="src/lib.rs" caption="Defining a `Summary` trait with a default implementation of the `summarize` method">

```rust,noplayground
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-14/src/lib.rs:here}}
```

<span class="caption">Listing 10-14: Defining a `Summary` trait with a default
implementation of the `summarize` method</span>
</Listing>

To use a default implementation to summarize instances of `NewsArticle`, we
specify an empty `impl` block with `impl Summary for NewsArticle {}`.
Expand Down Expand Up @@ -339,14 +336,13 @@ is a type alias for the type of the `impl` block, which in this case is
`cmp_display` method if its inner type `T` implements the `PartialOrd` trait
that enables comparison *and* the `Display` trait that enables printing.

<span class="filename">Filename: src/lib.rs</span>
<Listing number="10-15" file-name="src/lib.rs" caption="Conditionally implementing methods on a generic type depending on trait bounds">

```rust,noplayground
{{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-15/src/lib.rs}}
```

<span class="caption">Listing 10-15: Conditionally implementing methods on a
generic type depending on trait bounds</span>
</Listing>

We can also conditionally implement a trait for any type that implements
another trait. Implementations of a trait on any type that satisfies the trait
Expand Down
Loading