1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-06-27 19:18:59 +02:00

Cover Supertraits, Generic Traits (#1854)

This commit is contained in:
Dustin J. Mitchell
2024-03-12 09:49:39 -04:00
committed by GitHub
parent d0656ca90b
commit ac2cb44d54
5 changed files with 97 additions and 2 deletions

View File

@ -0,0 +1,52 @@
---
Minutes: 5
---
# Generic Traits
Traits can also be generic, just like types and functions. A trait's parameters
get concrete types when it is used.
```rust,editable
#[derive(Debug)]
struct Foo(String);
impl From<u32> for Foo {
fn from(from: u32) -> Foo {
Foo(format!("Converted from integer: {from}"))
}
}
impl From<bool> for Foo {
fn from(from: bool) -> Foo {
Foo(format!("Converted from bool: {from}"))
}
}
fn main() {
let from_int = Foo::from(123);
let from_bool = Foo::from(true);
println!("{from_int:?}, {from_bool:?}");
}
```
<details>
- The `From` trait will be covered later in the course, but its
[definition in the `std` docs](https://doc.rust-lang.org/std/convert/trait.From.html)
is simple.
- Implementations of the trait do not need to cover all possible type
parameters. Here, `Foo::From("hello")` would not compile because there is no
`From<&str>` implementation for `Foo`.
- Generic traits take types as "input", while associated types are a kind of
"output type. A trait can have multiple implementations for different input
types.
- In fact, Rust requires that at most one implementation of a trait match for
any type T. Unlike some other languages, Rust has no heuristic for choosing
the "most specific" match. There is work on adding this support, called
[specialization](https://rust-lang.github.io/rfcs/1210-impl-specialization.html).
</details>