Skip to main content
Simple and safe type conversions that may fail in a controlled way under some circumstances. This is useful when you are doing a type conversion that may trivially succeed but may also need special handling. For example, there is no way to convert an i64 into an i32 using the Into trait, because an i64 may contain a value that an i32 cannot represent and so the conversion would lose data. This might be handled by truncating the i64 to an i32 or by simply returning Bounded::::MAX, or by some other method. The Into trait is intended for perfect conversions, so the TryInto trait informs the programmer when a type conversion could go bad and lets them decide how to handle it.

Signature

Generic Implementations

  • TryInto is reflexive, which means that TryInto is implemented
  • TryInto is implemented for all types that implement Into

Examples

Converting chess coordinates (like ‘e4’) into a validated position:

Trait functions

try_into

Attempts to convert the input type T into the output type S. In the event of a conversion error, returns None.

Signature

Examples