Expand description
Traits for conversions between types.
The traits in this module provide a way to convert from one type to another type. Each trait serves a different purpose:
- Implement the
AsReftrait for cheap reference-to-reference conversions - Implement the
AsMuttrait for cheap mutable-to-mutable conversions - Implement the
Fromtrait for consuming value-to-value conversions that cannot fail. This automatically provides an implementation ofInto - Implement the
TryFromtrait for consuming value-to-value conversions that can fail. This automatically provides an implementation ofTryInto
The traits in this module are often used as trait bounds for generic functions such that arguments of multiple types are supported. See the documentation of each trait for examples.
As a library author, you should always prefer implementing From<T> or
TryFrom<T> rather than Into<U> or TryInto<U>,
as From and TryFrom provide greater flexibility and offer
equivalent Into or TryInto implementations for free, thanks to a
blanket implementation in the standard library. In versions of Rust prior to Rust 1.41,
it was sometimes necessary to implement Into or TryInto directly when converting to a
type outside the current crate.
§Generic Implementations
AsRefandAsMutauto-dereference if the inner type is a reference (but not generally for all dereferenceable types)From<U> for TimpliesInto<T> for UTryFrom<U> for TimpliesTryInto<T> for UFromandIntoare reflexive, which means that all types canintothemselves andfromthemselves
See each trait for usage examples.
Enums§
- Infallible
- The error type for errors that can never happen.
Traits§
- AsMut
- Used to do a cheap mutable-to-mutable reference conversion.
- AsRef
- Used to do a cheap reference-to-reference conversion.
- From
- Used to do value-to-value conversions while consuming the input value. It is the reciprocal of
Into. - Into
- A value-to-value conversion that consumes the input value. The
opposite of
From. - TryFrom
- Simple and safe type conversions that may fail in a controlled
way under some circumstances. It is the reciprocal of
TryInto. - TryInto
- An attempted conversion that consumes
self, which may or may not be expensive. - Float
ToInt Experimental - Supporting trait for inherent methods of
f32andf64such asto_int_unchecked. Typically doesn’t need to be used directly.
Functions§
- identity
- The identity function.