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
TryIntois reflexive, which means thatTryIntois implementedTryIntois implemented for all types that implementInto
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, returnsNone.