From: LukeMathWalker <20745048+LukeMathWalker@users.noreply.github.com> Date: Fri, 24 May 2024 10:43:34 +0000 (+0200) Subject: Restructure negative trait bounds section. X-Git-Url: http://git.euphorik.ch/index.cgi?a=commitdiff_plain;h=cbafcf2cd4acb56a699389e8679eecc94b208430;p=rust_exercises.git Restructure negative trait bounds section. --- 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`