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
AsRef
trait for cheap reference-to-reference conversions - Implement the
AsMut
trait for cheap mutable-to-mutable conversions - Implement the
From
trait for consuming value-to-value conversions - Implement the
Into
trait for consuming value-to-value conversions to types outside the current crate - The
TryFrom
andTryInto
traits behave likeFrom
andInto
, but should be implemented when the conversion can fail.
The traits in this module are often used as trait bounds for generic functions such that to 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. When targeting a version prior to Rust 1.41, it
may be necessary to implement Into
or TryInto
directly when converting to a type
outside the current crate.
Generic Implementations
AsRef
andAsMut
auto-dereference if the inner type is a reference (but not generally for all dereferenceable types)From
<U> for T
impliesInto
<T> for U
TryFrom
<U> for T
impliesTryInto
<T> for U
From
andInto
are reflexive, which means that all types caninto
themselves andfrom
themselves
See each trait for usage examples.
Enums
Traits
f32
and f64
such as to_int_unchecked
.
Typically doesn’t need to be used directly.Into
.TryInto
.self
, which may or may not be
expensive.