@@ -366,6 +366,11 @@ pub trait From<T>: Sized {
366366/// provides an equivalent `TryInto` implementation for free, thanks to a
367367/// blanket implementation in the standard library. For more information on this,
368368/// see the documentation for [`Into`].
369+ ///
370+ /// # Implementing `TryInto`
371+ ///
372+ /// This suffers the same restrictions and reasoning as implementing
373+ /// [`Into`], see there for details.
369374///
370375/// [`TryFrom`]: trait.TryFrom.html
371376/// [`Into`]: trait.Into.html
@@ -380,7 +385,44 @@ pub trait TryInto<T>: Sized {
380385 fn try_into ( self ) -> Result < T , Self :: Error > ;
381386}
382387
383- /// Attempt to construct `Self` via a conversion.
388+ /// Simple and safe type conversions that may fail in a controlled
389+ /// way under some circumstances. It is the reciprocal of [`TryInto`].
390+ ///
391+ /// This is useful when you are doing a type conversion that may
392+ /// trivially succeed but may also need special handling.
393+ /// For example, there is no way to convert an `i64` into an `i32`
394+ /// using the [`From`] trait, because an `i64` may contain a value
395+ /// that an `i32` cannot represent and so the conversion would lose data.
396+ /// This might be handled by truncating the `i64` to an `i32` (essentially
397+ /// giving the `i64`'s value modulo `i32::MAX`) or by simply returning
398+ /// `i32::MAX`, or by some other method. The `From` trait is intended
399+ /// for lossless conversions, so the `TryFrom` trait informs the
400+ /// programmer when a type conversion could go bad and lets them
401+ /// decide how to handle it.
402+ ///
403+ /// # Generic Implementations
404+ ///
405+ /// - `TryFrom<T> for U` implies [`TryInto<U>`]` for T`
406+ /// - [`try_from`] is reflexive, which means that `TryFrom<T> for T`
407+ /// is implemented
408+ ///
409+ /// # Examples
410+ ///
411+ /// As described, [`i32`] implements `TryFrom<i64>`:
412+ ///
413+ /// ```
414+ /// let big_number = 1_000_000_000_000i64;
415+ /// // Silently truncates `big_number`, requires detecting
416+ /// // and handling the truncation after the fact.
417+ /// let smaller_number = big_number as i32;
418+ /// assert_eq!(smaller_number, -727379968);
419+ ///
420+ /// let try_smaller_number = i32::try_from(big_number);
421+ /// assert!(try_smaller_number.is_err());
422+ ///
423+ /// let try_successful_smaller_number = i32::try_from(3);
424+ /// assert!(try_successful_smaller_number.is_ok());
425+ /// ```
384426#[ stable( feature = "try_from" , since = "1.34.0" ) ]
385427pub trait TryFrom < T > : Sized {
386428 /// The type returned in the event of a conversion error.
0 commit comments