Traits for conversions between types.
The traits in this module provide a general way to talk about conversions from one type to another. They follow the standard Rust conventions of as
/into
/from
.
Like many traits, these are often used as bounds for generic functions, to support arguments of multiple types.
As*
traits for reference-to-reference conversionsInto
trait when you want to consume the value in the conversionFrom
trait is the most flexible, useful for value and reference conversionsTryFrom
and TryInto
traits behave like From
and Into
, but allow for the conversion to failAs a library author, you should 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.
AsRef
and AsMut
auto-dereference if the inner type is a referenceFrom
<U> for T
implies Into
<T> for U
TryFrom
<U> for T
implies TryInto
<T> for U
From
and Into
are reflexive, which means that all types can into
themselves and from
themselvesSee each trait for usage examples.
Infallible | [ Experimental ] A type used as the error type for implementations of fallible conversion traits in cases where conversions cannot actually fail. |
AsMut | A cheap, mutable reference-to-mutable reference conversion. |
AsRef | A cheap reference-to-reference conversion. Used to convert a value to a reference value within generic code. |
From | Simple and safe type conversions in to |
Into | A conversion that consumes |
TryFrom | [ Experimental ] Attempt to construct |
TryInto | [ Experimental ] An attempted conversion that consumes |
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/convert/index.html