From 1400234843c70033c1c9c8b82ee0972a29da6f1c Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Fri, 28 Feb 2025 12:07:07 -0800 Subject: [PATCH] Move `From` definition into code block --- src/generics/generic-traits.md | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/generics/generic-traits.md b/src/generics/generic-traits.md index 40174f1d1eef..120068882f9c 100644 --- a/src/generics/generic-traits.md +++ b/src/generics/generic-traits.md @@ -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`][from] trait is +used to define type conversions: + +```rust +pub trait From: 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: Sized { - * fn from(value: T) -> Self; - * } - */ - impl From for Foo { fn from(from: u32) -> Foo { Foo(format!("Converted from integer: {from}")) @@ -40,9 +40,8 @@ fn main() {
-- 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 @@ -58,3 +57,5 @@ fn main() { [specialization](https://rust-lang.github.io/rfcs/1210-impl-specialization.html).
+ +[from]: https://doc.rust-lang.org/std/convert/trait.From.html