diff --git a/src/ch18-02-trait-objects.md b/src/ch18-02-trait-objects.md index b4cfe3e161..a4be46946c 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,8 +280,8 @@ error[E0308]: mismatched types | |_____^ expected `SelectBox`, found `Button` ``` -In Listing 17-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, +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 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: