Rollup merge of #31128 - kamalmarhubi:book-trait-impl-clarify, r=steveklabnik

This commit is contained in:
Steve Klabnik
2016-01-23 09:38:44 -05:00

View File

@@ -277,16 +277,22 @@ This will compile without error.
This means that even if someone does something bad like add methods to `i32`, This means that even if someone does something bad like add methods to `i32`,
it wont affect you, unless you `use` that trait. it wont affect you, unless you `use` that trait.
Theres one more restriction on implementing traits: either the trait, or the Theres one more restriction on implementing traits: either the trait
type youre writing the `impl` for, must be defined by you. So, we could or the type youre implementing it for must be defined by you. Or more
implement the `HasArea` type for `i32`, because `HasArea` is in our code. But precisely, one of them must be defined in the same crate as the `impl`
if we tried to implement `ToString`, a trait provided by Rust, for `i32`, we could you're writing. For more on Rust's module and package system, see the
not, because neither the trait nor the type are in our code. chapter on [crates and modules][cm].
So, we could implement the `HasArea` type for `i32`, because we defined
`HasArea` in our code. But if we tried to implement `ToString`, a trait
provided by Rust, for `i32`, we could not, because neither the trait nor
the type are defined in our crate.
One last thing about traits: generic functions with a trait bound use One last thing about traits: generic functions with a trait bound use
monomorphization (mono: one, morph: form), so they are statically dispatched. monomorphization (mono: one, morph: form), so they are statically dispatched.
Whats that mean? Check out the chapter on [trait objects][to] for more details. Whats that mean? Check out the chapter on [trait objects][to] for more details.
[cm]: crates-and-modules.html
[to]: trait-objects.html [to]: trait-objects.html
# Multiple trait bounds # Multiple trait bounds