Skip to content

Commit

Permalink
Merge pull request #3981 from SpectralPixel/listing-preprocessor-chap…
Browse files Browse the repository at this point in the history
…ter-08

Convert Listings in Chapter 08 to `<Listing>`
  • Loading branch information
chriskrycho authored Oct 15, 2024
2 parents 991e64c + 8aab8a2 commit 1bfea4f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 49 deletions.
50 changes: 30 additions & 20 deletions src/ch08-01-vectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ lines of text in a file or the prices of items in a shopping cart.
To create a new empty vector, we call the `Vec::new` function, as shown in
Listing 8-1.

<Listing number="8-1" caption="Creating a new, empty vector to hold values of type `i32`">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-01/src/main.rs:here}}
```

<span class="caption">Listing 8-1: Creating a new, empty vector to hold values
of type `i32`</span>
</Listing>

Note that we added a type annotation here. Because we aren’t inserting any
values into this vector, Rust doesn’t know what kind of elements we intend to
Expand All @@ -35,12 +36,13 @@ new vector that holds the values you give it. Listing 8-2 creates a new
because that’s the default integer type, as we discussed in the [“Data
Types”][data-types]<!-- ignore --> section of Chapter 3.

<Listing number="8-2" caption="Creating a new vector containing values">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-02/src/main.rs:here}}
```

<span class="caption">Listing 8-2: Creating a new vector containing
values</span>
</Listing>

Because we’ve given initial `i32` values, Rust can infer that the type of `v`
is `Vec<i32>`, and the type annotation isn’t necessary. Next, we’ll look at how
Expand All @@ -51,12 +53,13 @@ to modify a vector.
To create a vector and then add elements to it, we can use the `push` method,
as shown in Listing 8-3.

<Listing number="8-3" caption="Using the `push` method to add values to a vector">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-03/src/main.rs:here}}
```

<span class="caption">Listing 8-3: Using the `push` method to add values to a
vector</span>
</Listing>

As with any variable, if we want to be able to change its value, we need to
make it mutable using the `mut` keyword, as discussed in Chapter 3. The numbers
Expand All @@ -72,12 +75,13 @@ the values that are returned from these functions for extra clarity.
Listing 8-4 shows both methods of accessing a value in a vector, with indexing
syntax and the `get` method.

<Listing number="8-4" caption="Using indexing syntax and using the `get` method to access an item in a vector">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-04/src/main.rs:here}}
```

<span class="caption">Listing 8-4: Using indexing syntax and using the `get`
method to access an item in a vector</span>
</Listing>

Note a few details here. We use the index value of `2` to get the third element
because vectors are indexed by number, starting at zero. Using `&` and `[]`
Expand All @@ -91,12 +95,13 @@ existing elements. As an example, let’s see what happens when we have a vector
of five elements and then we try to access an element at index 100 with each
technique, as shown in Listing 8-5.

<Listing number="8-5" caption="Attempting to access the element at index 100 in a vector containing five elements">

```rust,should_panic,panics
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-05/src/main.rs:here}}
```

<span class="caption">Listing 8-5: Attempting to access the element at index
100 in a vector containing five elements</span>
</Listing>

When we run this code, the first `[]` method will cause the program to panic
because it references a nonexistent element. This method is best used when you
Expand All @@ -123,12 +128,13 @@ to the first element in a vector and try to add an element to the end. This
program won’t work if we also try to refer to that element later in the
function.

<Listing number="8-6" caption="Attempting to add an element to a vector while holding a reference to an item">

```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-06/src/main.rs:here}}
```

<span class="caption">Listing 8-6: Attempting to add an element to a vector
while holding a reference to an item</span>
</Listing>

Compiling this code will result in this error:

Expand Down Expand Up @@ -156,23 +162,25 @@ elements rather than use indices to access one at a time. Listing 8-7 shows how
to use a `for` loop to get immutable references to each element in a vector of
`i32` values and print them.

<Listing number="8-7" caption="Printing each element in a vector by iterating over the elements using a `for` loop">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-07/src/main.rs:here}}
```

<span class="caption">Listing 8-7: Printing each element in a vector by
iterating over the elements using a `for` loop</span>
</Listing>

We can also iterate over mutable references to each element in a mutable vector
in order to make changes to all the elements. The `for` loop in Listing 8-8
will add `50` to each element.

<Listing number="8-8" caption="Iterating over mutable references to elements in a vector">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-08/src/main.rs:here}}
```

<span class="caption">Listing 8-8: Iterating over mutable references to
elements in a vector</span>
</Listing>

To change the value that the mutable reference refers to, we have to use the
`*` dereference operator to get to the value in `i` before we can use the `+=`
Expand Down Expand Up @@ -202,12 +210,13 @@ value types, and all the enum variants will be considered the same type: that
of the enum. Then we can create a vector to hold that enum and so, ultimately,
hold different types. We’ve demonstrated this in Listing 8-9.

<Listing number="8-9" caption="Defining an `enum` to store values of different types in one vector">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-09/src/main.rs:here}}
```

<span class="caption">Listing 8-9: Defining an `enum` to store values of
different types in one vector</span>
</Listing>

Rust needs to know what types will be in the vector at compile time so it knows
exactly how much memory on the heap will be needed to store each element. We
Expand All @@ -231,12 +240,13 @@ addition to `push`, a `pop` method removes and returns the last element.
Like any other `struct`, a vector is freed when it goes out of scope, as
annotated in Listing 8-10.

<Listing number="8-10" caption="Showing where the vector and its elements are dropped">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-10/src/main.rs:here}}
```

<span class="caption">Listing 8-10: Showing where the vector and its elements
are dropped</span>
</Listing>

When the vector gets dropped, all of its contents are also dropped, meaning the
integers it holds will be cleaned up. The borrow checker ensures that any
Expand Down
44 changes: 27 additions & 17 deletions src/ch08-02-strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,37 +41,41 @@ of bytes with some extra guarantees, restrictions, and capabilities. An example
of a function that works the same way with `Vec<T>` and `String` is the `new`
function to create an instance, shown in Listing 8-11.

<Listing number="8-11" caption="Creating a new, empty `String`">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-11/src/main.rs:here}}
```

<span class="caption">Listing 8-11: Creating a new, empty `String`</span>
</Listing>

This line creates a new, empty string called `s`, into which we can then load
data. Often, we’ll have some initial data with which we want to start the
string. For that, we use the `to_string` method, which is available on any type
that implements the `Display` trait, as string literals do. Listing 8-12 shows
two examples.

<Listing number="8-12" caption="Using the `to_string` method to create a `String` from a string literal">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-12/src/main.rs:here}}
```

<span class="caption">Listing 8-12: Using the `to_string` method to create a
`String` from a string literal</span>
</Listing>

This code creates a string containing `initial contents`.

We can also use the function `String::from` to create a `String` from a string
literal. The code in Listing 8-13 is equivalent to the code in Listing 8-12
that uses `to_string`.

<Listing number="8-13" caption="Using the `String::from` function to create a `String` from a string literal">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-13/src/main.rs:here}}
```

<span class="caption">Listing 8-13: Using the `String::from` function to create
a `String` from a string literal</span>
</Listing>

Because strings are used for so many things, we can use many different generic
APIs for strings, providing us with a lot of options. Some of them can seem
Expand All @@ -82,12 +86,13 @@ readability.
Remember that strings are UTF-8 encoded, so we can include any properly encoded
data in them, as shown in Listing 8-14.

<Listing number="8-14" caption="Storing greetings in different languages in strings">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-14/src/main.rs:here}}
```

<span class="caption">Listing 8-14: Storing greetings in different languages in
strings</span>
</Listing>

All of these are valid `String` values.

Expand All @@ -102,24 +107,26 @@ use the `+` operator or the `format!` macro to concatenate `String` values.
We can grow a `String` by using the `push_str` method to append a string slice,
as shown in Listing 8-15.

<Listing number="8-15" caption="Appending a string slice to a `String` using the `push_str` method">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-15/src/main.rs:here}}
```

<span class="caption">Listing 8-15: Appending a string slice to a `String`
using the `push_str` method</span>
</Listing>

After these two lines, `s` will contain `foobar`. The `push_str` method takes a
string slice because we don’t necessarily want to take ownership of the
parameter. For example, in the code in Listing 8-16, we want to be able to use
`s2` after appending its contents to `s1`.

<Listing number="8-16" caption="Using a string slice after appending its contents to a `String`">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-16/src/main.rs:here}}
```

<span class="caption">Listing 8-16: Using a string slice after appending its
contents to a `String`</span>
</Listing>

If the `push_str` method took ownership of `s2`, we wouldn’t be able to print
its value on the last line. However, this code works as we’d expect!
Expand All @@ -128,12 +135,13 @@ The `push` method takes a single character as a parameter and adds it to the
`String`. Listing 8-17 adds the letter *l* to a `String` using the `push`
method.

<Listing number="8-17" caption="Adding one character to a `String` value using `push`">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-17/src/main.rs:here}}
```

<span class="caption">Listing 8-17: Adding one character to a `String` value
using `push`</span>
</Listing>

As a result, `s` will contain `lol`.

Expand All @@ -142,12 +150,13 @@ As a result, `s` will contain `lol`.
Often, you’ll want to combine two existing strings. One way to do so is to use
the `+` operator, as shown in Listing 8-18.

<Listing number="8-18" caption="Using the `+` operator to combine two `String` values into a new `String` value">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-18/src/main.rs:here}}
```

<span class="caption">Listing 8-18: Using the `+` operator to combine two
`String` values into a new `String` value</span>
</Listing>

The string `s3` will contain `Hello, world!`. The reason `s1` is no longer
valid after the addition, and the reason we used a reference to `s2`, has to do
Expand Down Expand Up @@ -215,12 +224,13 @@ string by referencing them by index is a valid and common operation. However,
if you try to access parts of a `String` using indexing syntax in Rust, you’ll
get an error. Consider the invalid code in Listing 8-19.

<Listing number="8-19" caption="Attempting to use indexing syntax with a String">

```rust,ignore,does_not_compile
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-19/src/main.rs:here}}
```

<span class="caption">Listing 8-19: Attempting to use indexing syntax with a
String</span>
</Listing>

This code will result in the following error:

Expand Down
30 changes: 18 additions & 12 deletions src/ch08-03-hash-maps.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ One way to create an empty hash map is to use `new` and to add elements with
names are *Blue* and *Yellow*. The Blue team starts with 10 points, and the
Yellow team starts with 50.

<Listing number="8-20" caption="Creating a new hash map and inserting some keys and values">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-20/src/main.rs:here}}
```

<span class="caption">Listing 8-20: Creating a new hash map and inserting some
keys and values</span>
</Listing>

Note that we need to first `use` the `HashMap` from the collections portion of
the standard library. Of our three common collections, this one is the least
Expand All @@ -47,12 +48,13 @@ must have the same type.
We can get a value out of the hash map by providing its key to the `get`
method, as shown in Listing 8-21.

<Listing number="8-21" caption="Accessing the score for the Blue team stored in the hash map">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-21/src/main.rs:here}}
```

<span class="caption">Listing 8-21: Accessing the score for the Blue team
stored in the hash map</span>
</Listing>

Here, `score` will have the value that’s associated with the Blue team, and the
result will be `10`. The `get` method returns an `Option<&V>`; if there’s no
Expand Down Expand Up @@ -81,12 +83,13 @@ For types that implement the `Copy` trait, like `i32`, the values are copied
into the hash map. For owned values like `String`, the values will be moved and
the hash map will be the owner of those values, as demonstrated in Listing 8-22.

<Listing number="8-22" caption="Showing that keys and values are owned by the hash map once they’re inserted">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-22/src/main.rs:here}}
```

<span class="caption">Listing 8-22: Showing that keys and values are owned by
the hash map once they’re inserted</span>
</Listing>

We aren’t able to use the variables `field_name` and `field_value` after
they’ve been moved into the hash map with the call to `insert`.
Expand Down Expand Up @@ -120,12 +123,13 @@ Even though the code in Listing 8-23 calls `insert` twice, the hash map will
only contain one key–value pair because we’re inserting the value for the Blue
team’s key both times.

<Listing number="8-23" caption="Replacing a value stored with a particular key">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-23/src/main.rs:here}}
```

<span class="caption">Listing 8-23: Replacing a value stored with a particular
key</span>
</Listing>

This code will print `{"Blue": 25}`. The original value of `10` has been
overwritten.
Expand All @@ -147,12 +151,13 @@ we want to check whether the key for the Yellow team has a value associated
with it. If it doesn’t, we want to insert the value `50`, and the same for the
Blue team. Using the `entry` API, the code looks like Listing 8-24.

<Listing number="8-24" caption="Using the `entry` method to only insert if the key does not already have a value">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-24/src/main.rs:here}}
```

<span class="caption">Listing 8-24: Using the `entry` method to only insert if
the key does not already have a value</span>
</Listing>

The `or_insert` method on `Entry` is defined to return a mutable reference to
the value for the corresponding `Entry` key if that key exists, and if not, it
Expand All @@ -175,12 +180,13 @@ the words as keys and increment the value to keep track of how many times we’v
seen that word. If it’s the first time we’ve seen a word, we’ll first insert
the value `0`.

<Listing number="8-25" caption="Counting occurrences of words using a hash map that stores words and counts">

```rust
{{#rustdoc_include ../listings/ch08-common-collections/listing-08-25/src/main.rs:here}}
```

<span class="caption">Listing 8-25: Counting occurrences of words using a hash
map that stores words and counts</span>
</Listing>

This code will print `{"world": 2, "hello": 1, "wonderful": 1}`. You might see
the same key–value pairs printed in a different order: recall from the
Expand Down

0 comments on commit 1bfea4f

Please sign in to comment.