Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ch17-02: Monomorphization applies to generics in general #3367

Merged
merged 4 commits into from
Dec 11, 2024
Merged
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
19 changes: 9 additions & 10 deletions src/ch18-02-trait-objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,15 @@ didn’t mean to pass and so should pass a different type or we should implement
### Trait Objects Perform Dynamic Dispatch

Recall in the [“Performance of Code Using
Generics”][performance-of-code-using-generics]<!-- ignore --> section in
Chapter 10 our discussion on the monomorphization process performed by the
compiler when we use trait bounds on generics: the compiler generates
nongeneric implementations of functions and methods for each concrete type that
we use in place of a generic type parameter. The code that results from
monomorphization is doing _static dispatch_, which is when the compiler knows
what method you’re calling at compile time. This is opposed to _dynamic
dispatch_, which is when the compiler can’t tell at compile time which method
you’re calling. In dynamic dispatch cases, the compiler emits code that at
runtime will figure out which method to call.
Generics”][performance-of-code-using-generics]<!-- ignore --> section in Chapter
10 our discussion on the monomorphization process performed on generics by the
compiler: the compiler generates nongeneric implementations of functions and
methods for each concrete type that we use in place of a generic type parameter.
The code that results from monomorphization is doing _static dispatch_, which is
when the compiler knows what method you’re calling at compile time. This is
opposed to _dynamic dispatch_, which is when the compiler can’t tell at compile
time which method you’re calling. In dynamic dispatch cases, the compiler emits
code that at runtime will figure out which method to call.

When we use trait objects, Rust must use dynamic dispatch. The compiler doesn’t
know all the types that might be used with the code that’s using trait objects,
Expand Down
Loading