From fc450cce552f9e6a7495c856c5b5eb2615bccb7d Mon Sep 17 00:00:00 2001 From: AthfanFasee Date: Tue, 1 Jul 2025 18:06:43 +0800 Subject: [PATCH 1/2] fix typos in chapter 18.2 (17-9 -> 18-9, 17-11 -> 18-11) --- src/ch18-02-trait-objects.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ch18-02-trait-objects.md b/src/ch18-02-trait-objects.md index b4cfe3e161..b56f49558b 100644 --- a/src/ch18-02-trait-objects.md +++ b/src/ch18-02-trait-objects.md @@ -247,7 +247,7 @@ let v = vec!["Hello world"]; ``` Type inference is trickier for trait objects. For example, say we tried to factor -the `components` array in Listing 17-9 into a separate variable, like this: +the `components` array in Listing 18-9 into a separate variable, like this: ```rust,ignore,does_not_compile fn main() { @@ -260,7 +260,7 @@ fn main() { } ``` -Listing 17-11: Factoring the components array causes a type error +Listing 18-11: Factoring the components array causes a type error This refactor causes the program to no longer compile! The compiler rejects this program with the following error: @@ -280,7 +280,7 @@ error[E0308]: mismatched types | |_____^ expected `SelectBox`, found `Button` ``` -In Listing 17-09, the compiler understands that the `components` vector must have the type +In Listing 18-09, the compiler understands that the `components` vector must have the type `Vec>` because that's specified in the `Screen` struct definition. But in Listing 17-11, the compiler loses that information at the point where `components` is defined. To fix the issue, you have to give a hint to the type inference algorithm. That can either be via an explicit cast on From 4802fc6279283fbcdc1f2e87957632319e555cb9 Mon Sep 17 00:00:00 2001 From: AthfanFasee Date: Tue, 1 Jul 2025 18:09:25 +0800 Subject: [PATCH 2/2] fix typo chapter 18.2 (17-11 -> 18-11) --- src/ch18-02-trait-objects.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ch18-02-trait-objects.md b/src/ch18-02-trait-objects.md index b56f49558b..a4be46946c 100644 --- a/src/ch18-02-trait-objects.md +++ b/src/ch18-02-trait-objects.md @@ -281,7 +281,7 @@ error[E0308]: mismatched types ``` In Listing 18-09, the compiler understands that the `components` vector must have the type -`Vec>` because that's specified in the `Screen` struct definition. But in Listing 17-11, +`Vec>` because that's specified in the `Screen` struct definition. But in Listing 18-11, the compiler loses that information at the point where `components` is defined. To fix the issue, you have to give a hint to the type inference algorithm. That can either be via an explicit cast on any element of the vector, like this: