From 89cb629051899696bf6106c427b00bbf522e0907 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 19 Jun 2024 16:30:02 +0200 Subject: [PATCH 01/31] Convert Listing 10-1 to `` --- src/ch10-00-generics.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ch10-00-generics.md b/src/ch10-00-generics.md index 56ceee20dc..6ac0939ea0 100644 --- a/src/ch10-00-generics.md +++ b/src/ch10-00-generics.md @@ -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. -Filename: src/main.rs + ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-01/src/main.rs:here}} ``` -Listing 10-1: Finding the largest number in a list of -numbers + 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 From c04c59bb0effd46696225575ed52cdc41f522474 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 19 Jun 2024 16:33:02 +0200 Subject: [PATCH 02/31] Convert Listing 10-2 to `` --- src/ch10-00-generics.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ch10-00-generics.md b/src/ch10-00-generics.md index 6ac0939ea0..6a2f1486d8 100644 --- a/src/ch10-00-generics.md +++ b/src/ch10-00-generics.md @@ -63,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. -Filename: src/main.rs + ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-02/src/main.rs}} ``` -Listing 10-2: Code to find the largest number in *two* -lists of numbers + 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 From d6c87013c77c9972fe82e4956c2c1739d4da01ae Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 19 Jun 2024 16:36:38 +0200 Subject: [PATCH 03/31] Convert Listing 10-3 to `` --- src/ch10-00-generics.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ch10-00-generics.md b/src/ch10-00-generics.md index 6a2f1486d8..565e6297e6 100644 --- a/src/ch10-00-generics.md +++ b/src/ch10-00-generics.md @@ -85,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. -Filename: src/main.rs + ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-03/src/main.rs:here}} ``` -Listing 10-3: Abstracted code to find the largest number -in two lists + 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, From 0e1f4004095659805f8f1ae708359c57f78f5a9f Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 19 Jun 2024 16:41:10 +0200 Subject: [PATCH 04/31] Fixed bugs in ``s 10-1 to 10-3 (inclusive) I missed the dash in `file-name`. I should probably start testing these before committing them... --- src/ch10-00-generics.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ch10-00-generics.md b/src/ch10-00-generics.md index 565e6297e6..2e68384e11 100644 --- a/src/ch10-00-generics.md +++ b/src/ch10-00-generics.md @@ -42,7 +42,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-01/src/main.rs:here}} @@ -63,7 +63,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-02/src/main.rs}} @@ -85,7 +85,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-03/src/main.rs:here}} From fc743102e1758286cbf22e23ad8d1bee9f33b7fd Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 19 Jun 2024 16:43:45 +0200 Subject: [PATCH 05/31] Convert Listing 10-4 to `` (Had to fix a conflict here) --- src/ch10-01-syntax.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ch10-01-syntax.md b/src/ch10-01-syntax.md index 0b71c04c5b..1466654c2a 100644 --- a/src/ch10-01-syntax.md +++ b/src/ch10-01-syntax.md @@ -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. -Filename: src/main.rs + ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-04/src/main.rs:here}} ``` -Listing 10-4: Two functions that differ only in their -names and in the types in their signatures + 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 From 0e7e50bc7753a436a3be04739a5c79ca3bc7f639 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 19 Jun 2024 16:45:59 +0200 Subject: [PATCH 06/31] Convert Listing 10-5 to `` (Had to fix a conflict here... come on, was changing the order of two words *really* necessary? ;D ) --- src/ch10-01-syntax.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ch10-01-syntax.md b/src/ch10-01-syntax.md index 1466654c2a..24b8f40313 100644 --- a/src/ch10-01-syntax.md +++ b/src/ch10-01-syntax.md @@ -57,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. -Filename: src/main.rs + ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/src/main.rs}} ``` -Listing 10-5: The `largest` function using generic type -parameters; this doesn’t compile yet + If we compile this code right now, we’ll get this error: From 07efb8f3723acf8d75a250214da82c258792ae05 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 19 Jun 2024 16:48:21 +0200 Subject: [PATCH 07/31] Convert Listing 10-6 to `` --- src/ch10-01-syntax.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ch10-01-syntax.md b/src/ch10-01-syntax.md index 24b8f40313..4b8e27267f 100644 --- a/src/ch10-01-syntax.md +++ b/src/ch10-01-syntax.md @@ -88,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` struct to hold `x` and `y` coordinate values of any type. -Filename: src/main.rs + ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-06/src/main.rs}} ``` -Listing 10-6: A `Point` struct that holds `x` and `y` -values of type `T` + 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 From 1a4046a88cdfa4672ec619e411bdf398c6a4c021 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 19 Jun 2024 16:50:29 +0200 Subject: [PATCH 08/31] Convert Listing 10-7 to `` --- src/ch10-01-syntax.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ch10-01-syntax.md b/src/ch10-01-syntax.md index 4b8e27267f..d7757ebbe5 100644 --- a/src/ch10-01-syntax.md +++ b/src/ch10-01-syntax.md @@ -108,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` that has values of different types, as in Listing 10-7, our code won’t compile. -Filename: src/main.rs + ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/src/main.rs}} ``` -Listing 10-7: The fields `x` and `y` must be the same -type because both have the same generic data type `T`. + 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 From 9de7f2dae308022f1d4e1102493031bf85b6fa21 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 19 Jun 2024 16:52:44 +0200 Subject: [PATCH 09/31] Convert Listing 10-8 to `` --- src/ch10-01-syntax.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ch10-01-syntax.md b/src/ch10-01-syntax.md index d7757ebbe5..5eff954a27 100644 --- a/src/ch10-01-syntax.md +++ b/src/ch10-01-syntax.md @@ -130,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`. -Filename: src/main.rs + ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-08/src/main.rs}} ``` -Listing 10-8: A `Point` generic over two types so -that `x` and `y` can be values of different types + 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 From e005c67ddbe30c158ec5399922ebd44eef3b239c Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 19 Jun 2024 16:54:45 +0200 Subject: [PATCH 10/31] Convert Listing 10-9 to `` --- src/ch10-01-syntax.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/ch10-01-syntax.md b/src/ch10-01-syntax.md index 5eff954a27..769567807e 100644 --- a/src/ch10-01-syntax.md +++ b/src/ch10-01-syntax.md @@ -193,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` struct we defined in Listing 10-6 with a method named `x` implemented on it. -Filename: src/main.rs + ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-09/src/main.rs}} ``` -Listing 10-9: Implementing a method named `x` on the -`Point` struct that will return a reference to the `x` field of type -`T` + Here, we’ve defined a method named `x` on `Point` that returns a reference to the data in the field `x`. From fd55a3b9ee4551783b67205f1d06b5f58180cc1d Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 19 Jun 2024 16:56:49 +0200 Subject: [PATCH 11/31] Convert Listing 10-10 to `` --- src/ch10-01-syntax.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ch10-01-syntax.md b/src/ch10-01-syntax.md index 769567807e..d6a60f905a 100644 --- a/src/ch10-01-syntax.md +++ b/src/ch10-01-syntax.md @@ -219,14 +219,13 @@ type. We could, for example, implement methods only on `Point` instances rather than on `Point` instances with any generic type. In Listing 10-10 we use the concrete type `f32`, meaning we don’t declare any types after `impl`. -Filename: src/main.rs + ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-10/src/main.rs:here}} ``` -Listing 10-10: An `impl` block that only applies to a -struct with a particular concrete type for the generic type parameter `T` + This code means the type `Point` will have a `distance_from_origin` method; other instances of `Point` where `T` is not of type `f32` will not From 3a8203eee73fea51fd9cf61184e1e0a61c0fdd13 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 19 Jun 2024 16:58:37 +0200 Subject: [PATCH 12/31] Convert Listing 10-10 to `` --- src/ch10-01-syntax.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ch10-01-syntax.md b/src/ch10-01-syntax.md index d6a60f905a..d823ad9681 100644 --- a/src/ch10-01-syntax.md +++ b/src/ch10-01-syntax.md @@ -240,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`). -Filename: src/main.rs + ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/src/main.rs}} ``` -Listing 10-11: A method that uses generic types different -from its struct’s definition + 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 From 0bc7e4a8d51ae7e75254b4b0c60c954c0072bb2f Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 19 Jun 2024 18:16:04 +0200 Subject: [PATCH 13/31] Convert Listing 10-12 to `` --- src/ch10-02-traits.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ch10-02-traits.md b/src/ch10-02-traits.md index 53e9b8115c..c1b82b69c0 100644 --- a/src/ch10-02-traits.md +++ b/src/ch10-02-traits.md @@ -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. -Filename: src/lib.rs + ```rust,noplayground {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-12/src/lib.rs}} ``` -Listing 10-12: A `Summary` trait that consists of the -behavior provided by a `summarize` method + 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 From c550845b6480192bedac9a0e9e8eddc60d8a25b0 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 19 Jun 2024 18:17:08 +0200 Subject: [PATCH 14/31] Convert Listing 10-13 to `` --- src/ch10-02-traits.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ch10-02-traits.md b/src/ch10-02-traits.md index c1b82b69c0..b02a935bd2 100644 --- a/src/ch10-02-traits.md +++ b/src/ch10-02-traits.md @@ -61,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. -Filename: src/lib.rs + ```rust,noplayground {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-13/src/lib.rs:here}} ``` -Listing 10-13: Implementing the `Summary` trait on the -`NewsArticle` and `Tweet` types + 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, From 6dff9026a16c97cb970868f1110ede39fcb15714 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 19 Jun 2024 18:20:26 +0200 Subject: [PATCH 15/31] Convert Listing 10-14 to `` --- src/ch10-02-traits.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ch10-02-traits.md b/src/ch10-02-traits.md index b02a935bd2..e41cbeb4ba 100644 --- a/src/ch10-02-traits.md +++ b/src/ch10-02-traits.md @@ -122,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. -Filename: src/lib.rs + ```rust,noplayground {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-14/src/lib.rs:here}} ``` -Listing 10-14: Defining a `Summary` trait with a default -implementation of the `summarize` method + To use a default implementation to summarize instances of `NewsArticle`, we specify an empty `impl` block with `impl Summary for NewsArticle {}`. From cb2d4696f09a8c61636e3d0c73dfc2e224aa273f Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 19 Jun 2024 20:15:45 +0200 Subject: [PATCH 16/31] Convert Listing 10-15 to `` --- src/ch10-02-traits.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ch10-02-traits.md b/src/ch10-02-traits.md index e41cbeb4ba..cfd4201066 100644 --- a/src/ch10-02-traits.md +++ b/src/ch10-02-traits.md @@ -336,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. -Filename: src/lib.rs + ```rust,noplayground {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-15/src/lib.rs}} ``` -Listing 10-15: Conditionally implementing methods on a -generic type depending on trait bounds + 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 From ed9eea7f40f54af68cd9477b749996dec5d8aa9c Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Mon, 24 Jun 2024 13:49:57 +0200 Subject: [PATCH 17/31] Convert Listing 10-16 to `` --- src/ch10-03-lifetime-syntax.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch10-03-lifetime-syntax.md b/src/ch10-03-lifetime-syntax.md index d327e512b9..bc0834b24a 100644 --- a/src/ch10-03-lifetime-syntax.md +++ b/src/ch10-03-lifetime-syntax.md @@ -26,12 +26,13 @@ program to reference data other than the data it’s intended to reference. Consider the program in Listing 10-16, which has an outer scope and an inner scope. ++ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/src/main.rs}} ``` -Listing 10-16: An attempt to use a reference whose value -has gone out of scope + > Note: The examples in Listing 10-16, 10-17, and 10-23 declare variables > without giving them an initial value, so the variable name exists in the outer From ef4e4514799e8d66d28d59568e704e201fa168d5 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 26 Jun 2024 13:41:29 +0200 Subject: [PATCH 18/31] Convert Listing 10-17 to `` --- src/ch10-03-lifetime-syntax.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch10-03-lifetime-syntax.md b/src/ch10-03-lifetime-syntax.md index bc0834b24a..7f67559821 100644 --- a/src/ch10-03-lifetime-syntax.md +++ b/src/ch10-03-lifetime-syntax.md @@ -66,12 +66,13 @@ The Rust compiler has a *borrow checker* that compares scopes to determine whether all borrows are valid. Listing 10-17 shows the same code as Listing 10-16 but with annotations showing the lifetimes of the variables. ++ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/src/main.rs}} ``` -Listing 10-17: Annotations of the lifetimes of `r` and -`x`, named `'a` and `'b`, respectively + Here, we’ve annotated the lifetime of `r` with `'a` and the lifetime of `x` with `'b`. As you can see, the inner `'b` block is much smaller than the outer From bdc62e9afca6d3fc371dfb1e548cc52cd3667d4a Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 26 Jun 2024 13:49:16 +0200 Subject: [PATCH 19/31] Convert Listing 10-18 to `` --- src/ch10-03-lifetime-syntax.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/ch10-03-lifetime-syntax.md b/src/ch10-03-lifetime-syntax.md index 7f67559821..fefe544dc2 100644 --- a/src/ch10-03-lifetime-syntax.md +++ b/src/ch10-03-lifetime-syntax.md @@ -84,12 +84,13 @@ with a lifetime of `'b`. The program is rejected because `'b` is shorter than Listing 10-18 fixes the code so it doesn’t have a dangling reference and it compiles without any errors. ++ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/src/main.rs}} ``` -Listing 10-18: A valid reference because the data has a -longer lifetime than the reference + Here, `x` has the lifetime `'b`, which in this case is larger than `'a`. This means `r` can reference `x` because Rust knows that the reference in `r` will From eb3423b5458992cc2b3a09b1b0b22cea8b71a348 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 26 Jun 2024 14:03:06 +0200 Subject: [PATCH 20/31] Convert Listing 10-19 to `` --- src/ch10-03-lifetime-syntax.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ch10-03-lifetime-syntax.md b/src/ch10-03-lifetime-syntax.md index fefe544dc2..393808328b 100644 --- a/src/ch10-03-lifetime-syntax.md +++ b/src/ch10-03-lifetime-syntax.md @@ -107,14 +107,13 @@ function will take two string slices and return a single string slice. After we’ve implemented the `longest` function, the code in Listing 10-19 should print `The longest string is abcd`. -Filename: src/main.rs + ```rust,ignore {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-19/src/main.rs}} ``` -Listing 10-19: A `main` function that calls the `longest` -function to find the longer of two string slices + Note that we want the function to take string slices, which are references, rather than strings, because we don’t want the `longest` function to take From b1cfe634a8ed7a8f6b2bde954b177f1546c2cc6e Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 26 Jun 2024 16:06:16 +0200 Subject: [PATCH 21/31] Convert Listing 10-20 to `` --- src/ch10-03-lifetime-syntax.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/ch10-03-lifetime-syntax.md b/src/ch10-03-lifetime-syntax.md index 393808328b..a843a26dfe 100644 --- a/src/ch10-03-lifetime-syntax.md +++ b/src/ch10-03-lifetime-syntax.md @@ -125,15 +125,13 @@ ones we want. If we try to implement the `longest` function as shown in Listing 10-20, it won’t compile. -Filename: src/main.rs + ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/src/main.rs:here}} ``` -Listing 10-20: An implementation of the `longest` -function that returns the longer of two string slices but does not yet -compile + Instead, we get the following error that talks about lifetimes: From 7d9e87f232eb8c6d1d27cef16056962cd6bfdb11 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 26 Jun 2024 16:10:18 +0200 Subject: [PATCH 22/31] Convert Listing 10-21 to `` --- src/ch10-03-lifetime-syntax.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/ch10-03-lifetime-syntax.md b/src/ch10-03-lifetime-syntax.md index a843a26dfe..38117d061f 100644 --- a/src/ch10-03-lifetime-syntax.md +++ b/src/ch10-03-lifetime-syntax.md @@ -197,15 +197,13 @@ relationship between lifetimes of the parameters and the return value. We’ll name the lifetime `'a` and then add it to each reference, as shown in Listing 10-21. -Filename: src/main.rs + ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-21/src/main.rs:here}} ``` -Listing 10-21: The `longest` function definition -specifying that all the references in the signature must have the same lifetime -`'a` + This code should compile and produce the result we want when we use it with the `main` function in Listing 10-19. From b91f208e2b856cf00291462c1e30ce7445b1d4e8 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 26 Jun 2024 16:10:49 +0200 Subject: [PATCH 23/31] Convert Listing 10-22 to `` --- src/ch10-03-lifetime-syntax.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ch10-03-lifetime-syntax.md b/src/ch10-03-lifetime-syntax.md index 38117d061f..2b0e7ae00e 100644 --- a/src/ch10-03-lifetime-syntax.md +++ b/src/ch10-03-lifetime-syntax.md @@ -247,14 +247,13 @@ Let’s look at how the lifetime annotations restrict the `longest` function by passing in references that have different concrete lifetimes. Listing 10-22 is a straightforward example. -Filename: src/main.rs + ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-22/src/main.rs:here}} ``` -Listing 10-22: Using the `longest` function with -references to `String` values that have different concrete lifetimes + In this example, `string1` is valid until the end of the outer scope, `string2` is valid until the end of the inner scope, and `result` references something From 704e4dd362a53507f03ee3c81924d141b6d15431 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 26 Jun 2024 16:12:23 +0200 Subject: [PATCH 24/31] Convert Listing 10-23 to `` --- src/ch10-03-lifetime-syntax.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ch10-03-lifetime-syntax.md b/src/ch10-03-lifetime-syntax.md index 2b0e7ae00e..3174a9571f 100644 --- a/src/ch10-03-lifetime-syntax.md +++ b/src/ch10-03-lifetime-syntax.md @@ -269,14 +269,13 @@ assignment of the value to the `result` variable inside the scope with inner scope, after the inner scope has ended. The code in Listing 10-23 will not compile. -Filename: src/main.rs + ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-23/src/main.rs:here}} ``` -Listing 10-23: Attempting to use `result` after `string2` -has gone out of scope + When we try to compile this code, we get this error: From d4d4a1df94d0a0650860553a51ba2ed84f446cf0 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 26 Jun 2024 16:15:47 +0200 Subject: [PATCH 25/31] Convert Listing 10-24 to `` --- src/ch10-03-lifetime-syntax.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ch10-03-lifetime-syntax.md b/src/ch10-03-lifetime-syntax.md index 3174a9571f..05235d7e0e 100644 --- a/src/ch10-03-lifetime-syntax.md +++ b/src/ch10-03-lifetime-syntax.md @@ -363,14 +363,13 @@ to hold references, but in that case we would need to add a lifetime annotation on every reference in the struct’s definition. Listing 10-24 has a struct named `ImportantExcerpt` that holds a string slice. -Filename: src/main.rs + ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-24/src/main.rs}} ``` -Listing 10-24: A struct that holds a reference, requiring -a lifetime annotation + This struct has the single field `part` that holds a string slice, which is a reference. As with generic data types, we declare the name of the generic From 36fb0ae9569749225d3a89c719aeaeb596a1b4c1 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 26 Jun 2024 16:19:24 +0200 Subject: [PATCH 26/31] Converted unnamed listing to `` (Chapter 10.3, 1/2) --- src/ch10-03-lifetime-syntax.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ch10-03-lifetime-syntax.md b/src/ch10-03-lifetime-syntax.md index 05235d7e0e..b7c9d15488 100644 --- a/src/ch10-03-lifetime-syntax.md +++ b/src/ch10-03-lifetime-syntax.md @@ -310,12 +310,14 @@ function is doing. For example, if we changed the implementation of the string slice, we wouldn’t need to specify a lifetime on the `y` parameter. The following code will compile: -Filename: src/main.rs + ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-08-only-one-reference-with-lifetime/src/main.rs:here}} ``` + + We’ve specified a lifetime parameter `'a` for the parameter `x` and the return type, but not for the parameter `y`, because the lifetime of `y` does not have any relationship with the lifetime of `x` or the return value. From b8c7c99ac002932700759b0f4f6056ac11f5d208 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 26 Jun 2024 16:20:35 +0200 Subject: [PATCH 27/31] Converted unnamed listing to `` (Chapter 10.3, 2/2) --- src/ch10-03-lifetime-syntax.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ch10-03-lifetime-syntax.md b/src/ch10-03-lifetime-syntax.md index b7c9d15488..ef956e6dba 100644 --- a/src/ch10-03-lifetime-syntax.md +++ b/src/ch10-03-lifetime-syntax.md @@ -330,12 +330,14 @@ reference because the value will go out of scope at the end of the function. Consider this attempted implementation of the `longest` function that won’t compile: -Filename: src/main.rs + ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/no-listing-09-unrelated-lifetime/src/main.rs:here}} ``` + + Here, even though we’ve specified a lifetime parameter `'a` for the return type, this implementation will fail to compile because the return value lifetime is not related to the lifetime of the parameters at all. Here is the From 9151143b899404cb9b3d19c31ba07f47b32a4fd5 Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 26 Jun 2024 16:23:15 +0200 Subject: [PATCH 28/31] Convert Listing 10-25 to `` --- src/ch10-03-lifetime-syntax.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/ch10-03-lifetime-syntax.md b/src/ch10-03-lifetime-syntax.md index ef956e6dba..1657a1979b 100644 --- a/src/ch10-03-lifetime-syntax.md +++ b/src/ch10-03-lifetime-syntax.md @@ -396,15 +396,13 @@ lifetime parameters for functions or structs that use references. However, we had a function in Listing 4-9, shown again in Listing 10-25, that compiled without lifetime annotations. -Filename: src/lib.rs + ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-25/src/main.rs:here}} ``` -Listing 10-25: A function we defined in Listing 4-9 that -compiled without lifetime annotations, even though the parameter and return -type are references + The reason this function compiles without lifetime annotations is historical: in early versions (pre-1.0) of Rust, this code wouldn’t have compiled because From e82cd3fb2c4cfac1fd8d6a4f3b1ef29e6f95146f Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 17 Jul 2024 10:27:22 +0200 Subject: [PATCH 29/31] Chapter 10 - 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. (Had to fix a conflict here) --- src/ch10-00-generics.md | 9 ++++++--- src/ch10-01-syntax.md | 26 ++++++++++++++++++-------- src/ch10-02-traits.md | 12 ++++++++---- src/ch10-03-lifetime-syntax.md | 34 ++++++++++++++++++++++++---------- 4 files changed, 56 insertions(+), 25 deletions(-) diff --git a/src/ch10-00-generics.md b/src/ch10-00-generics.md index 2e68384e11..87ad2d6f37 100644 --- a/src/ch10-00-generics.md +++ b/src/ch10-00-generics.md @@ -42,7 +42,8 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-01/src/main.rs:here}} @@ -63,7 +64,8 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-02/src/main.rs}} @@ -85,7 +87,8 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-03/src/main.rs:here}} diff --git a/src/ch10-01-syntax.md b/src/ch10-01-syntax.md index d823ad9681..3f8dd22164 100644 --- a/src/ch10-01-syntax.md +++ b/src/ch10-01-syntax.md @@ -16,7 +16,8 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-04/src/main.rs:here}} @@ -57,7 +58,8 @@ 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. -+ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/src/main.rs}} @@ -88,7 +90,8 @@ 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` struct to hold `x` and `y` coordinate values of any type. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-06/src/main.rs}} @@ -108,7 +111,8 @@ the fields `x` and `y` are *both* that same type, whatever that type may be. If we create an instance of a `Point` that has values of different types, as in Listing 10-7, our code won’t compile. -+ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/src/main.rs}} @@ -130,7 +134,8 @@ 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`. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-08/src/main.rs}} @@ -193,7 +198,9 @@ 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` struct we defined in Listing 10-6 with a method named `x` implemented on it. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-09/src/main.rs}} @@ -219,7 +226,9 @@ type. We could, for example, implement methods only on `Point` instances rather than on `Point` instances with any generic type. In Listing 10-10 we use the concrete type `f32`, meaning we don’t declare any types after `impl`. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-10/src/main.rs:here}} @@ -240,7 +249,8 @@ 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`). -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/src/main.rs}} diff --git a/src/ch10-02-traits.md b/src/ch10-02-traits.md index cfd4201066..fcb4dcd854 100644 --- a/src/ch10-02-traits.md +++ b/src/ch10-02-traits.md @@ -27,7 +27,8 @@ 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. -+ ```rust,noplayground {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-12/src/lib.rs}} @@ -61,7 +62,8 @@ 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. -+ ```rust,noplayground {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-13/src/lib.rs:here}} @@ -122,7 +124,8 @@ 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. -+ ```rust,noplayground {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-14/src/lib.rs:here}} @@ -336,7 +339,8 @@ 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. -+ ```rust,noplayground {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-15/src/lib.rs}} diff --git a/src/ch10-03-lifetime-syntax.md b/src/ch10-03-lifetime-syntax.md index 1657a1979b..469702d311 100644 --- a/src/ch10-03-lifetime-syntax.md +++ b/src/ch10-03-lifetime-syntax.md @@ -26,7 +26,8 @@ program to reference data other than the data it’s intended to reference. Consider the program in Listing 10-16, which has an outer scope and an inner scope. -+ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/src/main.rs}} @@ -66,7 +67,8 @@ The Rust compiler has a *borrow checker* that compares scopes to determine whether all borrows are valid. Listing 10-17 shows the same code as Listing 10-16 but with annotations showing the lifetimes of the variables. -+ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/src/main.rs}} @@ -84,7 +86,8 @@ with a lifetime of `'b`. The program is rejected because `'b` is shorter than Listing 10-18 fixes the code so it doesn’t have a dangling reference and it compiles without any errors. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/src/main.rs}} @@ -107,7 +110,8 @@ function will take two string slices and return a single string slice. After we’ve implemented the `longest` function, the code in Listing 10-19 should print `The longest string is abcd`. -+ ```rust,ignore {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-19/src/main.rs}} @@ -125,7 +129,9 @@ ones we want. If we try to implement the `longest` function as shown in Listing 10-20, it won’t compile. -+ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/src/main.rs:here}} @@ -197,7 +203,9 @@ relationship between lifetimes of the parameters and the return value. We’ll name the lifetime `'a` and then add it to each reference, as shown in Listing 10-21. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-21/src/main.rs:here}} @@ -247,7 +255,9 @@ Let’s look at how the lifetime annotations restrict the `longest` function by passing in references that have different concrete lifetimes. Listing 10-22 is a straightforward example. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-22/src/main.rs:here}} @@ -269,7 +279,8 @@ assignment of the value to the `result` variable inside the scope with inner scope, after the inner scope has ended. The code in Listing 10-23 will not compile. -+ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-23/src/main.rs:here}} @@ -367,7 +378,8 @@ to hold references, but in that case we would need to add a lifetime annotation on every reference in the struct’s definition. Listing 10-24 has a struct named `ImportantExcerpt` that holds a string slice. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-24/src/main.rs}} @@ -396,7 +408,9 @@ lifetime parameters for functions or structs that use references. However, we had a function in Listing 4-9, shown again in Listing 10-25, that compiled without lifetime annotations. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-25/src/main.rs:here}} From cddaed648313113a73f6fc65a117e9239256396f Mon Sep 17 00:00:00 2001 From: SpectralPixel Date: Wed, 17 Jul 2024 12:03:30 +0200 Subject: [PATCH 30/31] Convert unnamed `` (Chapter 10.1) --- src/ch10-01-syntax.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/ch10-01-syntax.md b/src/ch10-01-syntax.md index 3f8dd22164..6277e6e1c2 100644 --- a/src/ch10-01-syntax.md +++ b/src/ch10-01-syntax.md @@ -305,7 +305,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): -Filename: src/main.rs + ```rust enum Option_i32 { @@ -324,6 +324,8 @@ fn main() { } ``` + + The generic `Option` 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 From 8b443bcd9bbfa96450e28a34e191f1217dedba80 Mon Sep 17 00:00:00 2001 From: Chris Krycho Date: Tue, 15 Oct 2024 07:12:44 -0600 Subject: [PATCH 31/31] Back out "Chapter 10 - Wrap all ``s to comply with the virtual 80 character limit" This backs out commit e82cd3fb2c4cfac1fd8d6a4f3b1ef29e6f95146f. --- src/ch10-00-generics.md | 9 +++------ src/ch10-01-syntax.md | 26 ++++++++------------------ src/ch10-02-traits.md | 12 ++++-------- src/ch10-03-lifetime-syntax.md | 34 ++++++++++------------------------ 4 files changed, 25 insertions(+), 56 deletions(-) diff --git a/src/ch10-00-generics.md b/src/ch10-00-generics.md index 87ad2d6f37..2e68384e11 100644 --- a/src/ch10-00-generics.md +++ b/src/ch10-00-generics.md @@ -42,8 +42,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-01/src/main.rs:here}} @@ -64,8 +63,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-02/src/main.rs}} @@ -87,8 +85,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-03/src/main.rs:here}} diff --git a/src/ch10-01-syntax.md b/src/ch10-01-syntax.md index 6277e6e1c2..2a22baf581 100644 --- a/src/ch10-01-syntax.md +++ b/src/ch10-01-syntax.md @@ -16,8 +16,7 @@ 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. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-04/src/main.rs:here}} @@ -58,8 +57,7 @@ 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. -+ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-05/src/main.rs}} @@ -90,8 +88,7 @@ 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` struct to hold `x` and `y` coordinate values of any type. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-06/src/main.rs}} @@ -111,8 +108,7 @@ the fields `x` and `y` are *both* that same type, whatever that type may be. If we create an instance of a `Point` that has values of different types, as in Listing 10-7, our code won’t compile. -+ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-07/src/main.rs}} @@ -134,8 +130,7 @@ 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`. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-08/src/main.rs}} @@ -198,9 +193,7 @@ 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` struct we defined in Listing 10-6 with a method named `x` implemented on it. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-09/src/main.rs}} @@ -226,9 +219,7 @@ type. We could, for example, implement methods only on `Point` instances rather than on `Point` instances with any generic type. In Listing 10-10 we use the concrete type `f32`, meaning we don’t declare any types after `impl`. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-10/src/main.rs:here}} @@ -249,8 +240,7 @@ 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`). -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-11/src/main.rs}} diff --git a/src/ch10-02-traits.md b/src/ch10-02-traits.md index fcb4dcd854..cfd4201066 100644 --- a/src/ch10-02-traits.md +++ b/src/ch10-02-traits.md @@ -27,8 +27,7 @@ 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. -+ ```rust,noplayground {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-12/src/lib.rs}} @@ -62,8 +61,7 @@ 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. -+ ```rust,noplayground {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-13/src/lib.rs:here}} @@ -124,8 +122,7 @@ 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. -+ ```rust,noplayground {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-14/src/lib.rs:here}} @@ -339,8 +336,7 @@ 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. -+ ```rust,noplayground {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-15/src/lib.rs}} diff --git a/src/ch10-03-lifetime-syntax.md b/src/ch10-03-lifetime-syntax.md index 469702d311..1657a1979b 100644 --- a/src/ch10-03-lifetime-syntax.md +++ b/src/ch10-03-lifetime-syntax.md @@ -26,8 +26,7 @@ program to reference data other than the data it’s intended to reference. Consider the program in Listing 10-16, which has an outer scope and an inner scope. -+ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-16/src/main.rs}} @@ -67,8 +66,7 @@ The Rust compiler has a *borrow checker* that compares scopes to determine whether all borrows are valid. Listing 10-17 shows the same code as Listing 10-16 but with annotations showing the lifetimes of the variables. -+ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-17/src/main.rs}} @@ -86,8 +84,7 @@ with a lifetime of `'b`. The program is rejected because `'b` is shorter than Listing 10-18 fixes the code so it doesn’t have a dangling reference and it compiles without any errors. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-18/src/main.rs}} @@ -110,8 +107,7 @@ function will take two string slices and return a single string slice. After we’ve implemented the `longest` function, the code in Listing 10-19 should print `The longest string is abcd`. -+ ```rust,ignore {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-19/src/main.rs}} @@ -129,9 +125,7 @@ ones we want. If we try to implement the `longest` function as shown in Listing 10-20, it won’t compile. -+ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-20/src/main.rs:here}} @@ -203,9 +197,7 @@ relationship between lifetimes of the parameters and the return value. We’ll name the lifetime `'a` and then add it to each reference, as shown in Listing 10-21. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-21/src/main.rs:here}} @@ -255,9 +247,7 @@ Let’s look at how the lifetime annotations restrict the `longest` function by passing in references that have different concrete lifetimes. Listing 10-22 is a straightforward example. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-22/src/main.rs:here}} @@ -279,8 +269,7 @@ assignment of the value to the `result` variable inside the scope with inner scope, after the inner scope has ended. The code in Listing 10-23 will not compile. -+ ```rust,ignore,does_not_compile {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-23/src/main.rs:here}} @@ -378,8 +367,7 @@ to hold references, but in that case we would need to add a lifetime annotation on every reference in the struct’s definition. Listing 10-24 has a struct named `ImportantExcerpt` that holds a string slice. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-24/src/main.rs}} @@ -408,9 +396,7 @@ lifetime parameters for functions or structs that use references. However, we had a function in Listing 4-9, shown again in Listing 10-25, that compiled without lifetime annotations. -+ ```rust {{#rustdoc_include ../listings/ch10-generic-types-traits-and-lifetimes/listing-10-25/src/main.rs:here}}