Trait core::convert::From 1.0.0[−][src]
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. If the conversion can fail, 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);Run
While 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 by calling Into<CliError>::into
which is automatically provided when
implementing From
. The compiler then infers which implementation of Into
should be used.
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) }Run
Required methods
Loading content...Implementors
impl From<!> for Infallible
1.34.0[src]
impl From<!> for Infallible
1.34.0[src]impl From<!> for TryFromIntError
[src]
impl From<!> for TryFromIntError
[src]fn from(never: !) -> TryFromIntError
[src]
impl From<Infallible> for TryFromSliceError
1.36.0[src]
impl From<Infallible> for TryFromSliceError
1.36.0[src]fn from(x: Infallible) -> TryFromSliceError
[src]
impl From<Infallible> for TryFromIntError
1.34.0[src]
impl From<Infallible> for TryFromIntError
1.34.0[src]fn from(x: Infallible) -> TryFromIntError
[src]
impl From<NonZeroI8> for NonZeroI16
1.41.0[src]
impl From<NonZeroI8> for NonZeroI16
1.41.0[src]impl From<NonZeroI8> for NonZeroI32
1.41.0[src]
impl From<NonZeroI8> for NonZeroI32
1.41.0[src]impl From<NonZeroI8> for NonZeroI64
1.41.0[src]
impl From<NonZeroI8> for NonZeroI64
1.41.0[src]impl From<NonZeroI8> for NonZeroI128
1.41.0[src]
impl From<NonZeroI8> for NonZeroI128
1.41.0[src]impl From<NonZeroI8> for NonZeroIsize
1.41.0[src]
impl From<NonZeroI8> for NonZeroIsize
1.41.0[src]impl From<NonZeroI16> for NonZeroI32
1.41.0[src]
impl From<NonZeroI16> for NonZeroI32
1.41.0[src]fn from(small: NonZeroI16) -> Self
[src]
Converts NonZeroI16
to NonZeroI32
losslessly.
impl From<NonZeroI16> for NonZeroI64
1.41.0[src]
impl From<NonZeroI16> for NonZeroI64
1.41.0[src]fn from(small: NonZeroI16) -> Self
[src]
Converts NonZeroI16
to NonZeroI64
losslessly.
impl From<NonZeroI16> for NonZeroI128
1.41.0[src]
impl From<NonZeroI16> for NonZeroI128
1.41.0[src]fn from(small: NonZeroI16) -> Self
[src]
Converts NonZeroI16
to NonZeroI128
losslessly.
impl From<NonZeroI16> for NonZeroIsize
1.41.0[src]
impl From<NonZeroI16> for NonZeroIsize
1.41.0[src]fn from(small: NonZeroI16) -> Self
[src]
Converts NonZeroI16
to NonZeroIsize
losslessly.
impl From<NonZeroI16> for i16
1.31.0[src]
impl From<NonZeroI16> for i16
1.31.0[src]fn from(nonzero: NonZeroI16) -> Self
[src]
Converts a NonZeroI16
into an i16
impl From<NonZeroI32> for NonZeroI64
1.41.0[src]
impl From<NonZeroI32> for NonZeroI64
1.41.0[src]fn from(small: NonZeroI32) -> Self
[src]
Converts NonZeroI32
to NonZeroI64
losslessly.
impl From<NonZeroI32> for NonZeroI128
1.41.0[src]
impl From<NonZeroI32> for NonZeroI128
1.41.0[src]fn from(small: NonZeroI32) -> Self
[src]
Converts NonZeroI32
to NonZeroI128
losslessly.
impl From<NonZeroI32> for i32
1.31.0[src]
impl From<NonZeroI32> for i32
1.31.0[src]fn from(nonzero: NonZeroI32) -> Self
[src]
Converts a NonZeroI32
into an i32
impl From<NonZeroI64> for NonZeroI128
1.41.0[src]
impl From<NonZeroI64> for NonZeroI128
1.41.0[src]fn from(small: NonZeroI64) -> Self
[src]
Converts NonZeroI64
to NonZeroI128
losslessly.
impl From<NonZeroI64> for i64
1.31.0[src]
impl From<NonZeroI64> for i64
1.31.0[src]fn from(nonzero: NonZeroI64) -> Self
[src]
Converts a NonZeroI64
into an i64
impl From<NonZeroI128> for i128
1.31.0[src]
impl From<NonZeroI128> for i128
1.31.0[src]fn from(nonzero: NonZeroI128) -> Self
[src]
Converts a NonZeroI128
into an i128
impl From<NonZeroIsize> for isize
1.31.0[src]
impl From<NonZeroIsize> for isize
1.31.0[src]fn from(nonzero: NonZeroIsize) -> Self
[src]
Converts a NonZeroIsize
into an isize
impl From<NonZeroU8> for NonZeroI16
1.41.0[src]
impl From<NonZeroU8> for NonZeroI16
1.41.0[src]impl From<NonZeroU8> for NonZeroI32
1.41.0[src]
impl From<NonZeroU8> for NonZeroI32
1.41.0[src]impl From<NonZeroU8> for NonZeroI64
1.41.0[src]
impl From<NonZeroU8> for NonZeroI64
1.41.0[src]impl From<NonZeroU8> for NonZeroI128
1.41.0[src]
impl From<NonZeroU8> for NonZeroI128
1.41.0[src]impl From<NonZeroU8> for NonZeroIsize
1.41.0[src]
impl From<NonZeroU8> for NonZeroIsize
1.41.0[src]impl From<NonZeroU8> for NonZeroU16
1.41.0[src]
impl From<NonZeroU8> for NonZeroU16
1.41.0[src]impl From<NonZeroU8> for NonZeroU32
1.41.0[src]
impl From<NonZeroU8> for NonZeroU32
1.41.0[src]impl From<NonZeroU8> for NonZeroU64
1.41.0[src]
impl From<NonZeroU8> for NonZeroU64
1.41.0[src]impl From<NonZeroU8> for NonZeroU128
1.41.0[src]
impl From<NonZeroU8> for NonZeroU128
1.41.0[src]impl From<NonZeroU8> for NonZeroUsize
1.41.0[src]
impl From<NonZeroU8> for NonZeroUsize
1.41.0[src]impl From<NonZeroU16> for NonZeroI32
1.41.0[src]
impl From<NonZeroU16> for NonZeroI32
1.41.0[src]fn from(small: NonZeroU16) -> Self
[src]
Converts NonZeroU16
to NonZeroI32
losslessly.
impl From<NonZeroU16> for NonZeroI64
1.41.0[src]
impl From<NonZeroU16> for NonZeroI64
1.41.0[src]fn from(small: NonZeroU16) -> Self
[src]
Converts NonZeroU16
to NonZeroI64
losslessly.
impl From<NonZeroU16> for NonZeroI128
1.41.0[src]
impl From<NonZeroU16> for NonZeroI128
1.41.0[src]fn from(small: NonZeroU16) -> Self
[src]
Converts NonZeroU16
to NonZeroI128
losslessly.
impl From<NonZeroU16> for NonZeroU32
1.41.0[src]
impl From<NonZeroU16> for NonZeroU32
1.41.0[src]fn from(small: NonZeroU16) -> Self
[src]
Converts NonZeroU16
to NonZeroU32
losslessly.
impl From<NonZeroU16> for NonZeroU64
1.41.0[src]
impl From<NonZeroU16> for NonZeroU64
1.41.0[src]fn from(small: NonZeroU16) -> Self
[src]
Converts NonZeroU16
to NonZeroU64
losslessly.
impl From<NonZeroU16> for NonZeroU128
1.41.0[src]
impl From<NonZeroU16> for NonZeroU128
1.41.0[src]fn from(small: NonZeroU16) -> Self
[src]
Converts NonZeroU16
to NonZeroU128
losslessly.
impl From<NonZeroU16> for NonZeroUsize
1.41.0[src]
impl From<NonZeroU16> for NonZeroUsize
1.41.0[src]fn from(small: NonZeroU16) -> Self
[src]
Converts NonZeroU16
to NonZeroUsize
losslessly.
impl From<NonZeroU16> for u16
1.31.0[src]
impl From<NonZeroU16> for u16
1.31.0[src]fn from(nonzero: NonZeroU16) -> Self
[src]
Converts a NonZeroU16
into an u16
impl From<NonZeroU32> for NonZeroI64
1.41.0[src]
impl From<NonZeroU32> for NonZeroI64
1.41.0[src]fn from(small: NonZeroU32) -> Self
[src]
Converts NonZeroU32
to NonZeroI64
losslessly.
impl From<NonZeroU32> for NonZeroI128
1.41.0[src]
impl From<NonZeroU32> for NonZeroI128
1.41.0[src]fn from(small: NonZeroU32) -> Self
[src]
Converts NonZeroU32
to NonZeroI128
losslessly.
impl From<NonZeroU32> for NonZeroU64
1.41.0[src]
impl From<NonZeroU32> for NonZeroU64
1.41.0[src]fn from(small: NonZeroU32) -> Self
[src]
Converts NonZeroU32
to NonZeroU64
losslessly.
impl From<NonZeroU32> for NonZeroU128
1.41.0[src]
impl From<NonZeroU32> for NonZeroU128
1.41.0[src]fn from(small: NonZeroU32) -> Self
[src]
Converts NonZeroU32
to NonZeroU128
losslessly.
impl From<NonZeroU32> for u32
1.31.0[src]
impl From<NonZeroU32> for u32
1.31.0[src]fn from(nonzero: NonZeroU32) -> Self
[src]
Converts a NonZeroU32
into an u32
impl From<NonZeroU64> for NonZeroI128
1.41.0[src]
impl From<NonZeroU64> for NonZeroI128
1.41.0[src]fn from(small: NonZeroU64) -> Self
[src]
Converts NonZeroU64
to NonZeroI128
losslessly.
impl From<NonZeroU64> for NonZeroU128
1.41.0[src]
impl From<NonZeroU64> for NonZeroU128
1.41.0[src]fn from(small: NonZeroU64) -> Self
[src]
Converts NonZeroU64
to NonZeroU128
losslessly.
impl From<NonZeroU64> for u64
1.31.0[src]
impl From<NonZeroU64> for u64
1.31.0[src]fn from(nonzero: NonZeroU64) -> Self
[src]
Converts a NonZeroU64
into an u64
impl From<NonZeroU128> for u128
1.31.0[src]
impl From<NonZeroU128> for u128
1.31.0[src]fn from(nonzero: NonZeroU128) -> Self
[src]
Converts a NonZeroU128
into an u128
impl From<NonZeroUsize> for usize
1.31.0[src]
impl From<NonZeroUsize> for usize
1.31.0[src]fn from(nonzero: NonZeroUsize) -> Self
[src]
Converts a NonZeroUsize
into an usize
impl From<bool> for AtomicBool
1.24.0[src]
impl From<bool> for AtomicBool
1.24.0[src]impl From<isize> for AtomicIsize
1.23.0[src]
impl From<isize> for AtomicIsize
1.23.0[src]impl From<u8> for char
1.13.0[src]
impl From<u8> for char
1.13.0[src]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<usize> for AtomicUsize
1.23.0[src]
impl From<usize> for AtomicUsize
1.23.0[src]impl<'a, T> From<&'a Option<T>> for Option<&'a T>
1.30.0[src]
impl<'a, T> From<&'a Option<T>> for Option<&'a T>
1.30.0[src]fn from(o: &'a Option<T>) -> Option<&'a T>
[src]
Converts from &Option<T>
to Option<&T>
.
Examples
Converts an Option<
String
>
into an Option<
usize
>
, preserving the original.
The map
method takes the self
argument by value, consuming the original,
so this technique uses as_ref
to first take an Option
to a reference
to the value inside the original.
let s: Option<String> = Some(String::from("Hello, Rustaceans!")); let o: Option<usize> = Option::from(&s).map(|ss: &String| ss.len()); println!("Can still print s: {:?}", s); assert_eq!(o, Some(18));Run
impl<'a, T> From<&'a mut Option<T>> for Option<&'a mut T>
1.30.0[src]
impl<'a, T> From<&'a mut Option<T>> for Option<&'a mut T>
1.30.0[src]impl<T> From<!> for T
1.34.0[src]
impl<T> From<!> for T
1.34.0[src]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.
impl<T> From<T> for UnsafeCell<T>
1.12.0[src]
impl<T> From<T> for UnsafeCell<T>
1.12.0[src]