Skip to content

std: explain prefer TryInto over TryFrom when specifying traits bounds on generic function #140791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions library/core/src/convert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,8 @@ pub trait Into<T>: 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<T, E>`.
Expand Down Expand Up @@ -597,6 +597,9 @@ pub trait From<T>: 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
Expand Down Expand Up @@ -636,6 +639,9 @@ pub trait TryInto<T>: 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<T>` can be implemented as follows:
///
/// ```
Expand Down
Loading