Skip to content

Commit

Permalink
Move From definition into code block
Browse files Browse the repository at this point in the history
  • Loading branch information
randomPoison committed Feb 28, 2025
1 parent 611306a commit 1400234
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/generics/generic-traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ 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.
get concrete types when it is used. For example the [`From<T>`][from] trait is
used to define type conversions:

```rust
pub trait From<T>: Sized {
fn from(value: T) -> Self;
}
```

```rust,editable
#[derive(Debug)]
struct Foo(String);
/* https://doc.rust-lang.org/stable/std/convert/trait.From.html
*
* pub trait From<T>: Sized {
* fn from(value: T) -> Self;
* }
*/
impl From<u32> for Foo {
fn from(from: u32) -> Foo {
Foo(format!("Converted from integer: {from}"))
Expand All @@ -40,9 +40,8 @@ fn main() {

<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, and copied here for reference.
- The `From` trait will be covered later in the course, but its [definition in
the `std` docs][from] is simple, and copied here for reference.

- Implementations of the trait do not need to cover all possible type
parameters. Here, `Foo::from("hello")` would not compile because there is no
Expand All @@ -58,3 +57,5 @@ fn main() {
[specialization](https://rust-lang.github.io/rfcs/1210-impl-specialization.html).

</details>

[from]: https://doc.rust-lang.org/std/convert/trait.From.html

0 comments on commit 1400234

Please sign in to comment.