pub trait From<T>: Sized {
// Required method
fn from(value: T) -> Self;
}
Expand description
Used to do value-to-value conversions while consuming the input value. It is the reciprocal of
Into
.
One should always prefer implementing From
over Into
because implementing From
automatically provides one with an implementation of Into
thanks to the blanket implementation in the standard library.
Only implement Into
when targeting a version prior to Rust 1.41 and converting to a type
outside the current crate.
From
was not able to do these types of conversions in earlier versions because of Rust’s
orphaning rules.
See Into
for more details.
Prefer using Into
over using From
when specifying trait bounds on a generic function.
This way, types that directly implement Into
can be used as arguments as well.
The From
is also very useful when performing error handling. When constructing a function
that is capable of failing, the return type will generally be of the form Result<T, E>
.
The From
trait simplifies error handling by allowing a function to return a single error type
that encapsulate multiple error types. See the “Examples” section and the book for more
details.
Note: This trait must not fail. The From
trait is intended for perfect conversions.
If the conversion can fail or is not perfect, use TryFrom
.
Generic Implementations
From<T> for U
impliesInto
<U> for T
From
is reflexive, which means thatFrom<T> for T
is implemented
Examples
String
implements From<&str>
:
An explicit conversion from a &str
to a String is done as follows:
let string = "hello".to_string();
let other_string = String::from("hello");
assert_eq!(string, other_string);
RunWhile performing error handling it is often useful to implement From
for your own error type.
By converting underlying error types to our own custom error type that encapsulates the
underlying error type, we can return a single error type without losing information on the
underlying cause. The ‘?’ operator automatically converts the underlying error type to our
custom error type with From::from
.
use std::fs;
use std::io;
use std::num;
enum CliError {
IoError(io::Error),
ParseError(num::ParseIntError),
}
impl From<io::Error> for CliError {
fn from(error: io::Error) -> Self {
CliError::IoError(error)
}
}
impl From<num::ParseIntError> for CliError {
fn from(error: num::ParseIntError) -> Self {
CliError::ParseError(error)
}
}
fn open_and_parse_file(file_name: &str) -> Result<i32, CliError> {
let mut contents = fs::read_to_string(&file_name)?;
let num: i32 = contents.trim().parse()?;
Ok(num)
}
RunRequired Methods§
Implementors§
impl From<Infallible> for TryFromSliceError
impl From<Infallible> for TryFromIntError
impl From<bool> for f32
impl From<bool> for f64
impl From<bool> for i8
impl From<bool> for i16
impl From<bool> for i32
impl From<bool> for i64
impl From<bool> for i128
impl From<bool> for isize
impl From<bool> for u8
impl From<bool> for u16
impl From<bool> for u32
impl From<bool> for u64
impl From<bool> for u128
impl From<bool> for usize
impl From<bool> for AtomicBool
impl From<char> for u32
impl From<char> for u64
impl From<char> for u128
impl From<f32> for f64
impl From<i8> for f32
impl From<i8> for f64
impl From<i8> for i16
impl From<i8> for i32
impl From<i8> for i64
impl From<i8> for i128
impl From<i8> for isize
impl From<i8> for AtomicI8
impl From<i16> for f32
impl From<i16> for f64
impl From<i16> for i32
impl From<i16> for i64
impl From<i16> for i128
impl From<i16> for isize
impl From<i16> for AtomicI16
impl From<i32> for f64
impl From<i32> for i64
impl From<i32> for i128
impl From<i32> for AtomicI32
impl From<i64> for i128
impl From<i64> for AtomicI64
impl From<i128> for AtomicI128
impl From<isize> for AtomicIsize
impl From<!> for Infallible
impl From<!> for TryFromIntError
impl From<u8> for char
Maps a byte in 0x00..=0xFF to a char
whose code point has the same value, in U+0000..=U+00FF.
Unicode is designed such that this effectively decodes bytes with the character encoding that IANA calls ISO-8859-1. This encoding is compatible with ASCII.
Note that this is different from ISO/IEC 8859-1 a.k.a. ISO 8859-1 (with one less hyphen), which leaves some “blanks”, byte values that are not assigned to any character. ISO-8859-1 (the IANA one) assigns them to the C0 and C1 control codes.
Note that this is also different from Windows-1252 a.k.a. code page 1252, which is a superset ISO/IEC 8859-1 that assigns some (not all!) blanks to punctuation and various Latin characters.
To confuse things further, on the Web
ascii
, iso-8859-1
, and windows-1252
are all aliases
for a superset of Windows-1252 that fills the remaining blanks with corresponding
C0 and C1 control codes.
impl From<u8> for f32
impl From<u8> for f64
impl From<u8> for i16
impl From<u8> for i32
impl From<u8> for i64
impl From<u8> for i128
impl From<u8> for isize
impl From<u8> for u16
impl From<u8> for u32
impl From<u8> for u64
impl From<u8> for u128
impl From<u8> for usize
impl From<u8> for AtomicU8
impl From<u16> for f32
impl From<u16> for f64
impl From<u16> for i32
impl From<u16> for i64
impl From<u16> for i128
impl From<u16> for u32
impl From<u16> for u64
impl From<u16> for u128
impl From<u16> for usize
impl From<u16> for AtomicU16
impl From<u32> for f64
impl From<u32> for i64
impl From<u32> for i128
impl From<u32> for u64
impl From<u32> for u128
impl From<u32> for Ipv4Addr
impl From<u32> for AtomicU32
impl From<u64> for i128
impl From<u64> for u128
impl From<u64> for AtomicU64
impl From<u128> for Ipv6Addr
impl From<u128> for AtomicU128
impl From<usize> for AtomicUsize
impl From<float64x1_t> for Simd<f64, 1>
impl From<float64x2_t> for f64x2
impl From<float32x2_t> for f32x2
impl From<float32x4_t> for f32x4
impl From<int8x8_t> for i8x8
impl From<int8x16_t> for i8x16
impl From<int16x4_t> for i16x4
impl From<int16x8_t> for i16x8
impl From<int32x2_t> for i32x2
impl From<int32x4_t> for i32x4
impl From<int64x1_t> for Simd<i64, 1>
impl From<int64x2_t> for i64x2
impl From<poly8x8_t> for u8x8
impl From<poly8x16_t> for u8x16
impl From<poly16x4_t> for u16x4
impl From<poly16x8_t> for u16x8
impl From<poly64x1_t> for Simd<u64, 1>
impl From<poly64x2_t> for u64x2
impl From<uint8x8_t> for u8x8
impl From<uint8x16_t> for u8x16
impl From<uint16x4_t> for u16x4
impl From<uint16x8_t> for u16x8
impl From<uint32x2_t> for u32x2
impl From<uint32x4_t> for u32x4
impl From<uint64x1_t> for Simd<u64, 1>
impl From<uint64x2_t> for u64x2
impl From<Ipv4Addr> for IpAddr
impl From<Ipv4Addr> for u32
impl From<Ipv6Addr> for IpAddr
impl From<Ipv6Addr> for u128
impl From<SocketAddrV4> for SocketAddr
impl From<SocketAddrV6> for SocketAddr
impl From<NonZeroI8> for i8
impl From<NonZeroI8> for NonZeroI16
impl From<NonZeroI8> for NonZeroI32
impl From<NonZeroI8> for NonZeroI64
impl From<NonZeroI8> for NonZeroI128
impl From<NonZeroI8> for NonZeroIsize
impl From<NonZeroI16> for i16
impl From<NonZeroI16> for NonZeroI32
impl From<NonZeroI16> for NonZeroI64
impl From<NonZeroI16> for NonZeroI128
impl From<NonZeroI16> for NonZeroIsize
impl From<NonZeroI32> for i32
impl From<NonZeroI32> for NonZeroI64
impl From<NonZeroI32> for NonZeroI128
impl From<NonZeroI64> for i64
impl From<NonZeroI64> for NonZeroI128
impl From<NonZeroI128> for i128
impl From<NonZeroIsize> for isize
impl From<NonZeroU8> for u8
impl From<NonZeroU8> for NonZeroI16
impl From<NonZeroU8> for NonZeroI32
impl From<NonZeroU8> for NonZeroI64
impl From<NonZeroU8> for NonZeroI128
impl From<NonZeroU8> for NonZeroIsize
impl From<NonZeroU8> for NonZeroU16
impl From<NonZeroU8> for NonZeroU32
impl From<NonZeroU8> for NonZeroU64
impl From<NonZeroU8> for NonZeroU128
impl From<NonZeroU8> for NonZeroUsize
impl From<NonZeroU16> for u16
impl From<NonZeroU16> for NonZeroI32
impl From<NonZeroU16> for NonZeroI64
impl From<NonZeroU16> for NonZeroI128
impl From<NonZeroU16> for NonZeroU32
impl From<NonZeroU16> for NonZeroU64
impl From<NonZeroU16> for NonZeroU128
impl From<NonZeroU16> for NonZeroUsize
impl From<NonZeroU32> for u32
impl From<NonZeroU32> for NonZeroI64
impl From<NonZeroU32> for NonZeroI128
impl From<NonZeroU32> for NonZeroU64
impl From<NonZeroU32> for NonZeroU128
impl From<NonZeroU64> for u64
impl From<NonZeroU64> for NonZeroI128
impl From<NonZeroU64> for NonZeroU128
impl From<NonZeroU128> for u128
impl From<NonZeroUsize> for usize
impl From<Alignment> for usize
impl From<Alignment> for NonZeroUsize
impl From<Simd<f32, 2>> for float32x2_t
impl From<Simd<f32, 4>> for float32x4_t
impl From<Simd<f64, 1>> for float64x1_t
impl From<Simd<f64, 2>> for float64x2_t
impl From<Simd<i8, 8>> for int8x8_t
impl From<Simd<i8, 16>> for int8x16_t
impl From<Simd<i16, 4>> for int16x4_t
impl From<Simd<i16, 8>> for int16x8_t
impl From<Simd<i32, 2>> for int32x2_t
impl From<Simd<i32, 4>> for int32x4_t
impl From<Simd<i64, 1>> for int64x1_t
impl From<Simd<i64, 2>> for int64x2_t
impl From<Simd<u8, 8>> for poly8x8_t
impl From<Simd<u8, 8>> for uint8x8_t
impl From<Simd<u8, 16>> for poly8x16_t
impl From<Simd<u8, 16>> for uint8x16_t
impl From<Simd<u16, 4>> for poly16x4_t
impl From<Simd<u16, 4>> for uint16x4_t
impl From<Simd<u16, 8>> for poly16x8_t
impl From<Simd<u16, 8>> for uint16x8_t
impl From<Simd<u32, 2>> for uint32x2_t
impl From<Simd<u32, 4>> for uint32x4_t
impl From<Simd<u64, 1>> for poly64x1_t
impl From<Simd<u64, 1>> for uint64x1_t
impl From<Simd<u64, 2>> for poly64x2_t
impl From<Simd<u64, 2>> for uint64x2_t
impl From<[u8; 4]> for IpAddr
impl From<[u8; 4]> for Ipv4Addr
impl From<[u8; 16]> for IpAddr
impl From<[u8; 16]> for Ipv6Addr
impl From<[u16; 8]> for IpAddr
impl From<[u16; 8]> for Ipv6Addr
impl<'a, T> From<&'a Option<T>> for Option<&'a T>
impl<'a, T> From<&'a mut Option<T>> for Option<&'a mut T>
impl<I: Into<IpAddr>> From<(I, u16)> for SocketAddr
impl<T> From<[T; 1]> for (T,)
impl<T> From<[T; 2]> for (T, T)
impl<T> From<[T; 3]> for (T, T, T)
impl<T> From<[T; 4]> for (T, T, T, T)
impl<T> From<[T; 5]> for (T, T, T, T, T)
impl<T> From<[T; 6]> for (T, T, T, T, T, T)
impl<T> From<[T; 7]> for (T, T, T, T, T, T, T)
impl<T> From<[T; 8]> for (T, T, T, T, T, T, T, T)
impl<T> From<[T; 9]> for (T, T, T, T, T, T, T, T, T)
impl<T> From<[T; 10]> for (T, T, T, T, T, T, T, T, T, T)
impl<T> From<[T; 11]> for (T, T, T, T, T, T, T, T, T, T, T)
impl<T> From<[T; 12]> for (T, T, T, T, T, T, T, T, T, T, T, T)
impl<T> From<!> for T
Stability note: This impl does not yet exist, but we are “reserving space” to add it in the future. See rust-lang/rust#64715 for details.