From cbafcf2cd4acb56a699389e8679eecc94b208430 Mon Sep 17 00:00:00 2001 From: LukeMathWalker <20745048+LukeMathWalker@users.noreply.github.com> Date: Fri, 24 May 2024 12:43:34 +0200 Subject: [PATCH] Restructure negative trait bounds section. --- book/src/04_traits/09_from.md | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/book/src/04_traits/09_from.md b/book/src/04_traits/09_from.md index df54bfc..7724581 100644 --- a/book/src/04_traits/09_from.md +++ b/book/src/04_traits/09_from.md @@ -69,7 +69,20 @@ pub struct Foo } ``` -You can opt out of this behavior by using a **negative trait bound**: +In the case of `From`, the trait definition is equivalent to: + +```rust +pub trait From: Sized { + fn from(value: T) -> Self; +} +``` + +In other words, _both_ `T` and the type implementing `From` must be `Sized`, even +though the former bound is implicit. + +### Negative trait bounds + +You can opt out of the implicit `Sized` bound with a **negative trait bound**: ```rust pub struct Foo { @@ -82,8 +95,6 @@ pub struct Foo { This syntax reads as "`T` may or may not be `Sized`", and it allows you to bind `T` to a DST (e.g. `Foo`). It is a special case, though: negative trait bounds are exclusive to `Sized`, you can't use them with other traits. -In the case of `From`, we want _both_ `T` and the type implementing `From` to be `Sized`, even -though the former bound is implicit. ## `&str` to `String`