From b6c2a429efd85cbfa5f592c66faac6955729c6ea Mon Sep 17 00:00:00 2001 From: xizheyin Date: Thu, 8 May 2025 18:09:29 +0800 Subject: [PATCH 1/2] std: Make consistence between `From` and `Into` Signed-off-by: xizheyin --- library/core/src/convert/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/core/src/convert/mod.rs b/library/core/src/convert/mod.rs index e1b10e1074d27..ef184e1ceb4f1 100644 --- a/library/core/src/convert/mod.rs +++ b/library/core/src/convert/mod.rs @@ -464,8 +464,8 @@ pub trait Into: Sized { /// orphaning rules. /// See [`Into`] for more details. /// -/// Prefer using [`Into`] over using `From` when specifying trait bounds on a generic function. -/// This way, types that directly implement [`Into`] can be used as arguments as well. +/// Prefer using [`Into`] over [`From`] when specifying trait bounds on a generic function +/// to ensure that types that only implement [`Into`] can be used as well. /// /// The `From` trait is also very useful when performing error handling. When constructing a function /// that is capable of failing, the return type will generally be of the form `Result`. From 4101d90818b7e5574d5366e24c995980fa2de866 Mon Sep 17 00:00:00 2001 From: xizheyin Date: Thu, 8 May 2025 18:12:05 +0800 Subject: [PATCH 2/2] std: Explain prefer `TryInto` over `TryFrom` when specifying traits bounds on generic function Signed-off-by: xizheyin --- library/core/src/convert/mod.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/library/core/src/convert/mod.rs b/library/core/src/convert/mod.rs index ef184e1ceb4f1..c542a28beb87c 100644 --- a/library/core/src/convert/mod.rs +++ b/library/core/src/convert/mod.rs @@ -597,6 +597,9 @@ pub trait From: Sized { /// standard library. For more information on this, see the /// documentation for [`Into`]. /// +/// Prefer using [`TryInto`] over [`TryFrom`] when specifying trait bounds on a generic function +/// to ensure that types that only implement [`TryInto`] can be used as well. +/// /// # Implementing `TryInto` /// /// This suffers the same restrictions and reasoning as implementing @@ -636,6 +639,9 @@ pub trait TryInto: Sized { /// When the [`!`] type is stabilized [`Infallible`] and [`!`] will be /// equivalent. /// +/// Prefer using [`TryInto`] over [`TryFrom`] when specifying trait bounds on a generic function +/// to ensure that types that only implement [`TryInto`] can be used as well. +/// /// `TryFrom` can be implemented as follows: /// /// ```