From 68c60cb01498345bed2a01ff7609fa0aa9ffd871 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Sun, 26 May 2024 17:00:52 +0200 Subject: [PATCH 01/27] Convert Listing 8-1 to `` --- src/ch08-01-vectors.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-01-vectors.md b/src/ch08-01-vectors.md index ece5d59dce..40793ad352 100644 --- a/src/ch08-01-vectors.md +++ b/src/ch08-01-vectors.md @@ -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. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-01/src/main.rs:here}} ``` -Listing 8-1: Creating a new, empty vector to hold values -of type `i32` + 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 From 7e0d98d2998453d64dbefb80c0a7293dfadccc6b Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Sun, 26 May 2024 17:02:55 +0200 Subject: [PATCH 02/27] Convert Listing 8-2 to `` --- src/ch08-01-vectors.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-01-vectors.md b/src/ch08-01-vectors.md index 40793ad352..30595d2277 100644 --- a/src/ch08-01-vectors.md +++ b/src/ch08-01-vectors.md @@ -36,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] section of Chapter 3. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-02/src/main.rs:here}} ``` -Listing 8-2: Creating a new vector containing -values + Because we’ve given initial `i32` values, Rust can infer that the type of `v` is `Vec`, and the type annotation isn’t necessary. Next, we’ll look at how From 62b29107212e14f367fb7f0ac2a52cc634e33043 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Sun, 26 May 2024 17:04:36 +0200 Subject: [PATCH 03/27] Convert Listing 8-3 to `` --- src/ch08-01-vectors.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-01-vectors.md b/src/ch08-01-vectors.md index 30595d2277..cf2c2e7bb7 100644 --- a/src/ch08-01-vectors.md +++ b/src/ch08-01-vectors.md @@ -53,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. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-03/src/main.rs:here}} ``` -Listing 8-3: Using the `push` method to add values to a -vector + 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 From d09b84c273f060ed68cb109d6df4d6881c95c26f Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Sun, 26 May 2024 17:06:52 +0200 Subject: [PATCH 04/27] Convert Listing 8-4 to `` --- src/ch08-01-vectors.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-01-vectors.md b/src/ch08-01-vectors.md index cf2c2e7bb7..a583374f4c 100644 --- a/src/ch08-01-vectors.md +++ b/src/ch08-01-vectors.md @@ -75,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. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-04/src/main.rs:here}} ``` -Listing 8-4: Using indexing syntax and using the `get` -method to access an item in a vector + 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 `[]` From d3e2527a77cb7859776bc8ea5e1dd419c472154d Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Sun, 26 May 2024 17:08:40 +0200 Subject: [PATCH 05/27] Convert Listing 8-5 to `` --- src/ch08-01-vectors.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-01-vectors.md b/src/ch08-01-vectors.md index a583374f4c..5541b637f4 100644 --- a/src/ch08-01-vectors.md +++ b/src/ch08-01-vectors.md @@ -95,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. ++ ```rust,should_panic,panics {{#rustdoc_include ../listings/ch08-common-collections/listing-08-05/src/main.rs:here}} ``` -Listing 8-5: Attempting to access the element at index -100 in a vector containing five elements + 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 From 86f647af198ddab147dfde9ee53e21d384752552 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Sun, 26 May 2024 17:10:54 +0200 Subject: [PATCH 06/27] Convert Listing 8-6 to `` --- src/ch08-01-vectors.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-01-vectors.md b/src/ch08-01-vectors.md index 5541b637f4..8189afb9f2 100644 --- a/src/ch08-01-vectors.md +++ b/src/ch08-01-vectors.md @@ -128,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. ++ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch08-common-collections/listing-08-06/src/main.rs:here}} ``` -Listing 8-6: Attempting to add an element to a vector -while holding a reference to an item + Compiling this code will result in this error: From 51597b62ddb4ac094e38b0f0fbd92b51f7debdd1 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Sun, 26 May 2024 17:13:42 +0200 Subject: [PATCH 07/27] Convert Listing 8-7 to `` --- src/ch08-01-vectors.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-01-vectors.md b/src/ch08-01-vectors.md index 8189afb9f2..aa32ff6147 100644 --- a/src/ch08-01-vectors.md +++ b/src/ch08-01-vectors.md @@ -162,12 +162,13 @@ 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. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-07/src/main.rs:here}} ``` -Listing 8-7: Printing each element in a vector by -iterating over the elements using a `for` loop + 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 From 3029bd0d65e2353c25d12c9bfce79cc5c16315b3 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Sun, 26 May 2024 17:16:06 +0200 Subject: [PATCH 08/27] Convert Listing 8-8 to `` --- src/ch08-01-vectors.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-01-vectors.md b/src/ch08-01-vectors.md index aa32ff6147..64142ab3a4 100644 --- a/src/ch08-01-vectors.md +++ b/src/ch08-01-vectors.md @@ -174,12 +174,13 @@ 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. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-08/src/main.rs:here}} ``` -Listing 8-8: Iterating over mutable references to -elements in a vector + 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 `+=` From a179f406c4ea8ee255d1c3740941196ce0070f8b Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Sun, 26 May 2024 17:18:52 +0200 Subject: [PATCH 09/27] Convert Listing 8-9 to `` --- src/ch08-01-vectors.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-01-vectors.md b/src/ch08-01-vectors.md index 64142ab3a4..df4717aafc 100644 --- a/src/ch08-01-vectors.md +++ b/src/ch08-01-vectors.md @@ -210,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. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-09/src/main.rs:here}} ``` -Listing 8-9: Defining an `enum` to store values of -different types in one vector + 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 From fbf4c5075db7f8b102735a29149d01f224799a6a Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Sun, 26 May 2024 17:21:52 +0200 Subject: [PATCH 10/27] Convert Listing 8-10 to `` --- src/ch08-01-vectors.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-01-vectors.md b/src/ch08-01-vectors.md index df4717aafc..718cd597a3 100644 --- a/src/ch08-01-vectors.md +++ b/src/ch08-01-vectors.md @@ -240,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. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-10/src/main.rs:here}} ``` -Listing 8-10: Showing where the vector and its elements -are dropped + 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 From c4b601068faa972c49b272a82e5cc830e337e18b Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Sun, 26 May 2024 17:27:21 +0200 Subject: [PATCH 11/27] Convert Listing 8-11 to `` --- src/ch08-02-strings.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ch08-02-strings.md b/src/ch08-02-strings.md index be0c874748..aec81de9fa 100644 --- a/src/ch08-02-strings.md +++ b/src/ch08-02-strings.md @@ -41,11 +41,13 @@ of bytes with some extra guarantees, restrictions, and capabilities. An example of a function that works the same way with `Vec` and `String` is the `new` function to create an instance, shown in Listing 8-11. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-11/src/main.rs:here}} ``` -Listing 8-11: Creating a new, empty `String` + 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 From a66cccee27ee733262d0fd64746e872dd2a7e546 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Sun, 26 May 2024 17:28:44 +0200 Subject: [PATCH 12/27] Convert Listing 8-12 to `` --- src/ch08-02-strings.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-02-strings.md b/src/ch08-02-strings.md index aec81de9fa..c951cf51f6 100644 --- a/src/ch08-02-strings.md +++ b/src/ch08-02-strings.md @@ -55,12 +55,13 @@ 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. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-12/src/main.rs:here}} ``` -Listing 8-12: Using the `to_string` method to create a -`String` from a string literal + This code creates a string containing `initial contents`. From ec6737b32d95aa43ed8ae983c969288319f85b8c Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Sun, 26 May 2024 17:30:26 +0200 Subject: [PATCH 13/27] Convert Listing 8-13 to `` --- src/ch08-02-strings.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-02-strings.md b/src/ch08-02-strings.md index c951cf51f6..65f006b4be 100644 --- a/src/ch08-02-strings.md +++ b/src/ch08-02-strings.md @@ -69,12 +69,13 @@ 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`. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-13/src/main.rs:here}} ``` -Listing 8-13: Using the `String::from` function to create -a `String` from a string literal + 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 From ed4e0a9ed725027e39a6715e81c425eaf8ba1ce2 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Sun, 26 May 2024 17:32:09 +0200 Subject: [PATCH 14/27] Convert Listing 8-14 to `` --- src/ch08-02-strings.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-02-strings.md b/src/ch08-02-strings.md index 65f006b4be..2af543468b 100644 --- a/src/ch08-02-strings.md +++ b/src/ch08-02-strings.md @@ -86,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. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-14/src/main.rs:here}} ``` -Listing 8-14: Storing greetings in different languages in -strings + All of these are valid `String` values. From 211dd0ad81fc5c3753b55473b101fea120b9faff Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Sun, 26 May 2024 17:34:20 +0200 Subject: [PATCH 15/27] Convert Listing 8-15 to `` --- src/ch08-02-strings.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ch08-02-strings.md b/src/ch08-02-strings.md index 2af543468b..cc1df979a7 100644 --- a/src/ch08-02-strings.md +++ b/src/ch08-02-strings.md @@ -107,12 +107,14 @@ 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. ++ + ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-15/src/main.rs:here}} ``` -Listing 8-15: Appending a string slice to a `String` -using the `push_str` method + 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 From d593065169549eaa636fb1e8dde537c98a75afbc Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Sun, 26 May 2024 17:50:23 +0200 Subject: [PATCH 16/27] Convert Listing 8-16 to `` --- src/ch08-02-strings.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ch08-02-strings.md b/src/ch08-02-strings.md index cc1df979a7..b838ef6d13 100644 --- a/src/ch08-02-strings.md +++ b/src/ch08-02-strings.md @@ -109,7 +109,6 @@ as shown in Listing 8-15. - ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-15/src/main.rs:here}} ``` @@ -121,12 +120,13 @@ 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`. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-16/src/main.rs:here}} ``` -Listing 8-16: Using a string slice after appending its -contents to a `String` + 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! From 2778c4fdcb59a30556d8740c45cf96529169c3b9 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Mon, 27 May 2024 18:56:05 +0200 Subject: [PATCH 17/27] Convert Listing 8-17 to `` I understand why "Any estimates that you make at the coffee machine will come back to haunt you". I shouldn't've claimed 5 chapters. --- src/ch08-02-strings.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-02-strings.md b/src/ch08-02-strings.md index b838ef6d13..00919a79d9 100644 --- a/src/ch08-02-strings.md +++ b/src/ch08-02-strings.md @@ -135,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. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-17/src/main.rs:here}} ``` -Listing 8-17: Adding one character to a `String` value -using `push` + As a result, `s` will contain `lol`. From e1af4c12811edb3cdf5624f72727c0c56a6de444 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Mon, 27 May 2024 18:59:11 +0200 Subject: [PATCH 18/27] Convert Listing 8-18 to `` --- src/ch08-02-strings.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-02-strings.md b/src/ch08-02-strings.md index 00919a79d9..cce644b295 100644 --- a/src/ch08-02-strings.md +++ b/src/ch08-02-strings.md @@ -150,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. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-18/src/main.rs:here}} ``` -Listing 8-18: Using the `+` operator to combine two -`String` values into a new `String` value + 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 From c2632f73cd85ea7900a83b8fb72dfe66879aab42 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Mon, 27 May 2024 19:01:30 +0200 Subject: [PATCH 19/27] Convert Listing 8-19 to `` --- src/ch08-02-strings.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-02-strings.md b/src/ch08-02-strings.md index cce644b295..9494fd94cb 100644 --- a/src/ch08-02-strings.md +++ b/src/ch08-02-strings.md @@ -224,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. ++ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch08-common-collections/listing-08-19/src/main.rs:here}} ``` -Listing 8-19: Attempting to use indexing syntax with a -String + This code will result in the following error: From f4105cbb35d9f93a772a72e7afd768942ebeb74f Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Mon, 27 May 2024 19:04:04 +0200 Subject: [PATCH 20/27] Convert Listing 8-20 to `` --- src/ch08-03-hash-maps.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-03-hash-maps.md b/src/ch08-03-hash-maps.md index 72331e5b91..f41ce8b73f 100644 --- a/src/ch08-03-hash-maps.md +++ b/src/ch08-03-hash-maps.md @@ -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. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-20/src/main.rs:here}} ``` -Listing 8-20: Creating a new hash map and inserting some -keys and values + 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 From f9756188debb9e76eef570cfadee262a3629db00 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Mon, 27 May 2024 19:05:43 +0200 Subject: [PATCH 21/27] Convert Listing 8-21 to `` --- src/ch08-03-hash-maps.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-03-hash-maps.md b/src/ch08-03-hash-maps.md index f41ce8b73f..008f64f136 100644 --- a/src/ch08-03-hash-maps.md +++ b/src/ch08-03-hash-maps.md @@ -48,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. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-21/src/main.rs:here}} ``` -Listing 8-21: Accessing the score for the Blue team -stored in the hash map + 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 From 4cb8ab0ada146946e09a152bba812420d683fd11 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Mon, 27 May 2024 19:16:16 +0200 Subject: [PATCH 22/27] Convert Listing 8-22 to `` --- src/ch08-03-hash-maps.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-03-hash-maps.md b/src/ch08-03-hash-maps.md index 008f64f136..d3842ddc19 100644 --- a/src/ch08-03-hash-maps.md +++ b/src/ch08-03-hash-maps.md @@ -83,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. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-22/src/main.rs:here}} ``` -Listing 8-22: Showing that keys and values are owned by -the hash map once they’re inserted + 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`. From d306a72aff3633556ed36d09a1db3dc865282417 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Mon, 27 May 2024 19:17:57 +0200 Subject: [PATCH 23/27] Convert Listing 8-23 to `` --- src/ch08-03-hash-maps.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-03-hash-maps.md b/src/ch08-03-hash-maps.md index d3842ddc19..53a62adfcc 100644 --- a/src/ch08-03-hash-maps.md +++ b/src/ch08-03-hash-maps.md @@ -123,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. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-23/src/main.rs:here}} ``` -Listing 8-23: Replacing a value stored with a particular -key + This code will print `{"Blue": 25}`. The original value of `10` has been overwritten. From 36d7d209659a4a7761fbfcd07c0ef1d1d674f87f Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Mon, 27 May 2024 19:20:08 +0200 Subject: [PATCH 24/27] Convert Listing 8-24 to `` --- src/ch08-03-hash-maps.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-03-hash-maps.md b/src/ch08-03-hash-maps.md index 53a62adfcc..49bfd48386 100644 --- a/src/ch08-03-hash-maps.md +++ b/src/ch08-03-hash-maps.md @@ -151,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. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-24/src/main.rs:here}} ``` -Listing 8-24: Using the `entry` method to only insert if -the key does not already have a value + 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 From 3678c0f238264812099f392387fd1240bc02ae8a Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Mon, 27 May 2024 19:21:38 +0200 Subject: [PATCH 25/27] Convert Listing 8-25 to `` --- src/ch08-03-hash-maps.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch08-03-hash-maps.md b/src/ch08-03-hash-maps.md index 49bfd48386..057501415a 100644 --- a/src/ch08-03-hash-maps.md +++ b/src/ch08-03-hash-maps.md @@ -180,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`. ++ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-25/src/main.rs:here}} ``` -Listing 8-25: Counting occurrences of words using a hash -map that stores words and counts + 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 From 2b3b9f537034e95408a50bcf24c62bc30f5a3463 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 17 Jul 2024 10:24:30 +0200 Subject: [PATCH 26/27] Chapter 8 - Wrap all ``s to comply with the virtual 80 character limit More infos and concerns can be found in the equivalent commit, in the chapter 6 PR. --- src/ch08-01-vectors.md | 21 ++++++++++++++------- src/ch08-02-strings.md | 24 ++++++++++++++++-------- src/ch08-03-hash-maps.md | 18 ++++++++++++------ 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/src/ch08-01-vectors.md b/src/ch08-01-vectors.md index 718cd597a3..fcec18f17f 100644 --- a/src/ch08-01-vectors.md +++ b/src/ch08-01-vectors.md @@ -11,7 +11,8 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-01/src/main.rs:here}} @@ -53,7 +54,8 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-03/src/main.rs:here}} @@ -75,7 +77,8 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-04/src/main.rs:here}} @@ -95,7 +98,8 @@ 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. -+ ```rust,should_panic,panics {{#rustdoc_include ../listings/ch08-common-collections/listing-08-05/src/main.rs:here}} @@ -128,7 +132,8 @@ 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. -+ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch08-common-collections/listing-08-06/src/main.rs:here}} @@ -162,7 +167,8 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-07/src/main.rs:here}} @@ -174,7 +180,8 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-08/src/main.rs:here}} diff --git a/src/ch08-02-strings.md b/src/ch08-02-strings.md index 9494fd94cb..8f8464cc77 100644 --- a/src/ch08-02-strings.md +++ b/src/ch08-02-strings.md @@ -55,7 +55,8 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-12/src/main.rs:here}} @@ -69,7 +70,8 @@ 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`. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-13/src/main.rs:here}} @@ -86,7 +88,8 @@ readability. Remember that strings are UTF-8 encoded, so we can include any properly encoded data in them, as shown in Listing 8-14. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-14/src/main.rs:here}} @@ -107,7 +110,8 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-15/src/main.rs:here}} @@ -120,7 +124,8 @@ 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`. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-16/src/main.rs:here}} @@ -135,7 +140,8 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-17/src/main.rs:here}} @@ -150,7 +156,8 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-18/src/main.rs:here}} @@ -224,7 +231,8 @@ 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. -+ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch08-common-collections/listing-08-19/src/main.rs:here}} diff --git a/src/ch08-03-hash-maps.md b/src/ch08-03-hash-maps.md index 057501415a..c4327a019e 100644 --- a/src/ch08-03-hash-maps.md +++ b/src/ch08-03-hash-maps.md @@ -24,7 +24,8 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-20/src/main.rs:here}} @@ -48,7 +49,8 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-21/src/main.rs:here}} @@ -83,7 +85,8 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-22/src/main.rs:here}} @@ -123,7 +126,8 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-23/src/main.rs:here}} @@ -151,7 +155,8 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-24/src/main.rs:here}} @@ -180,7 +185,8 @@ 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`. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-25/src/main.rs:here}} From 8aab8a2115ba310d847dafa3b939c569ffe19281 Mon Sep 17 00:00:00 2001 From: Chris Krycho Date: Tue, 15 Oct 2024 07:09:59 -0600 Subject: [PATCH 27/27] Back out "Chapter 8 - Wrap all ``s to comply with the virtual 80 character limit" This backs out commit 2b3b9f537034e95408a50bcf24c62bc30f5a3463. --- src/ch08-01-vectors.md | 21 +++++++-------------- src/ch08-02-strings.md | 24 ++++++++---------------- src/ch08-03-hash-maps.md | 18 ++++++------------ 3 files changed, 21 insertions(+), 42 deletions(-) diff --git a/src/ch08-01-vectors.md b/src/ch08-01-vectors.md index fcec18f17f..718cd597a3 100644 --- a/src/ch08-01-vectors.md +++ b/src/ch08-01-vectors.md @@ -11,8 +11,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-01/src/main.rs:here}} @@ -54,8 +53,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-03/src/main.rs:here}} @@ -77,8 +75,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-04/src/main.rs:here}} @@ -98,8 +95,7 @@ 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. -+ ```rust,should_panic,panics {{#rustdoc_include ../listings/ch08-common-collections/listing-08-05/src/main.rs:here}} @@ -132,8 +128,7 @@ 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. -+ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch08-common-collections/listing-08-06/src/main.rs:here}} @@ -167,8 +162,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-07/src/main.rs:here}} @@ -180,8 +174,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-08/src/main.rs:here}} diff --git a/src/ch08-02-strings.md b/src/ch08-02-strings.md index 8f8464cc77..9494fd94cb 100644 --- a/src/ch08-02-strings.md +++ b/src/ch08-02-strings.md @@ -55,8 +55,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-12/src/main.rs:here}} @@ -70,8 +69,7 @@ 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`. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-13/src/main.rs:here}} @@ -88,8 +86,7 @@ readability. Remember that strings are UTF-8 encoded, so we can include any properly encoded data in them, as shown in Listing 8-14. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-14/src/main.rs:here}} @@ -110,8 +107,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-15/src/main.rs:here}} @@ -124,8 +120,7 @@ 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`. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-16/src/main.rs:here}} @@ -140,8 +135,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-17/src/main.rs:here}} @@ -156,8 +150,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-18/src/main.rs:here}} @@ -231,8 +224,7 @@ 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. -+ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch08-common-collections/listing-08-19/src/main.rs:here}} diff --git a/src/ch08-03-hash-maps.md b/src/ch08-03-hash-maps.md index c4327a019e..057501415a 100644 --- a/src/ch08-03-hash-maps.md +++ b/src/ch08-03-hash-maps.md @@ -24,8 +24,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-20/src/main.rs:here}} @@ -49,8 +48,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-21/src/main.rs:here}} @@ -85,8 +83,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-22/src/main.rs:here}} @@ -126,8 +123,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-23/src/main.rs:here}} @@ -155,8 +151,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-24/src/main.rs:here}} @@ -185,8 +180,7 @@ 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`. -+ ```rust {{#rustdoc_include ../listings/ch08-common-collections/listing-08-25/src/main.rs:here}}