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:
AsRef
trait for cheap reference-to-reference conversionsAsMut
trait for cheap mutable-to-mutable conversionsFrom
trait for consuming value-to-value conversionsInto
trait for consuming value-to-value conversions to types outside the current crateTryFrom
and TryInto
traits behave like From
and Into
, 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.
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 |
The error type for errors that can never happen. |
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 |
A value-to-value conversion that consumes the input value. The opposite of |
TryFrom |
Simple and safe type conversions that may fail in a controlled way under some circumstances. It is the reciprocal of |
TryInto |
An attempted conversion that consumes |
FloatToInt |
Experimental Supporting trait for inherent methods of |
identity |
The identity function. |
© 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