Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/ch18-02-trait-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -260,7 +260,7 @@ fn main() {
}
```

<span class="caption">Listing 17-11: Factoring the components array causes a type error</span>
<span class="caption">Listing 18-11: Factoring the components array causes a type error</span>

This refactor causes the program to no longer compile! The compiler rejects this program with
the following error:
Expand All @@ -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<Box<dyn Draw>>` 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<Box<dyn Draw>>` 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:
Expand Down