@@ -361,11 +361,17 @@ pub trait From<T>: Sized {
361361/// An attempted conversion that consumes `self`, which may or may not be
362362/// expensive.
363363///
364- /// Library authors should not directly implement this trait, but should prefer
365- /// implementing the [`TryFrom`] trait, which offers greater flexibility and
366- /// provides an equivalent `TryInto` implementation for free, thanks to a
367- /// blanket implementation in the standard library. For more information on this,
368- /// see the documentation for [`Into`].
364+ /// Library authors should usually not directly implement this trait,
365+ /// but should prefer implementing the [`TryFrom`] trait, which offers
366+ /// greater flexibility and provides an equivalent `TryInto`
367+ /// implementation for free, thanks to a blanket implementation in the
368+ /// standard library. For more information on this, see the
369+ /// documentation for [`Into`].
370+ ///
371+ /// # Implementing `TryInto`
372+ ///
373+ /// This suffers the same restrictions and reasoning as implementing
374+ /// [`Into`], see there for details.
369375///
370376/// [`TryFrom`]: trait.TryFrom.html
371377/// [`Into`]: trait.Into.html
@@ -380,7 +386,55 @@ pub trait TryInto<T>: Sized {
380386 fn try_into ( self ) -> Result < T , Self :: Error > ;
381387}
382388
383- /// Attempt to construct `Self` via a conversion.
389+ /// Simple and safe type conversions that may fail in a controlled
390+ /// way under some circumstances. It is the reciprocal of [`TryInto`].
391+ ///
392+ /// This is useful when you are doing a type conversion that may
393+ /// trivially succeed but may also need special handling.
394+ /// For example, there is no way to convert an `i64` into an `i32`
395+ /// using the [`From`] trait, because an `i64` may contain a value
396+ /// that an `i32` cannot represent and so the conversion would lose data.
397+ /// This might be handled by truncating the `i64` to an `i32` (essentially
398+ /// giving the `i64`'s value modulo `i32::MAX`) or by simply returning
399+ /// `i32::MAX`, or by some other method. The `From` trait is intended
400+ /// for perfect conversions, so the `TryFrom` trait informs the
401+ /// programmer when a type conversion could go bad and lets them
402+ /// decide how to handle it.
403+ ///
404+ /// # Generic Implementations
405+ ///
406+ /// - `TryFrom<T> for U` implies [`TryInto<U>`]` for T`
407+ /// - [`try_from`] is reflexive, which means that `TryFrom<T> for T`
408+ /// is implemented and cannot fail -- the associated `Error` type for
409+ /// calling `T::try_from()` on a value of type `T` is `Infallible`.
410+ /// When the `!` type is stablized `Infallible` and `!` will be
411+ /// equivalent.
412+ ///
413+ /// # Examples
414+ ///
415+ /// As described, [`i32`] implements `TryFrom<i64>`:
416+ ///
417+ /// ```
418+ /// use std::convert::TryFrom;
419+ ///
420+ /// let big_number = 1_000_000_000_000i64;
421+ /// // Silently truncates `big_number`, requires detecting
422+ /// // and handling the truncation after the fact.
423+ /// let smaller_number = big_number as i32;
424+ /// assert_eq!(smaller_number, -727379968);
425+ ///
426+ /// // Returns an error because `big_number` is too big to
427+ /// // fit in an `i32`.
428+ /// let try_smaller_number = i32::try_from(big_number);
429+ /// assert!(try_smaller_number.is_err());
430+ ///
431+ /// // Returns `Ok(3)`.
432+ /// let try_successful_smaller_number = i32::try_from(3);
433+ /// assert!(try_successful_smaller_number.is_ok());
434+ /// ```
435+ ///
436+ /// [`try_from`]: trait.TryFrom.html#tymethod.try_from
437+ /// [`TryInto`]: trait.TryInto.html
384438#[ stable( feature = "try_from" , since = "1.34.0" ) ]
385439pub trait TryFrom < T > : Sized {
386440 /// The type returned in the event of a conversion error.
0 commit comments