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);
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)
}
Required Methods§
Implementors§
impl From<&str> for Box<str, Global>
impl From<&str> for Box<dyn Error + 'static, Global>
impl From<&str> for Rc<str>
impl From<&str> for String
impl From<&str> for Arc<str>
impl From<&str> for Vec<u8, Global>
impl From<&CStr> for Box<CStr, Global>
impl From<&CStr> for CString
impl From<&CStr> for Rc<CStr>
impl From<&CStr> for Arc<CStr>
impl From<&String> for String
impl From<&StreamResult> for Result<MZStatus, MZError>
impl From<&OsStr> for Box<OsStr, Global>
impl From<&OsStr> for Rc<OsStr>
impl From<&OsStr> for Arc<OsStr>
impl From<&Path> for Box<Path, Global>
impl From<&Path> for Rc<Path>
impl From<&Path> for Arc<Path>
impl From<&mut str> for String
impl From<WasmError> for CompileError
impl From<WasmType> for ValType
impl From<Infallible> for TryFromSliceError
impl From<Infallible> for TryFromIntError
impl From<Cow<'_, str>> for Box<str, Global>
impl From<Cow<'_, CStr>> for Box<CStr, Global>
impl From<Cow<'_, OsStr>> for Box<OsStr, Global>
impl From<Cow<'_, Path>> for Box<Path, Global>
impl From<TryReserveErrorKind> for TryReserveError
impl From<ErrorKind> for std::io::error::Error
Intended for use for errors not exposed to the user, where allocating onto the heap (for normal construction via Error::new) is too costly.
impl From<Pointer> for u64
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<char> for String
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<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<u8> for ExitCode
impl From<u8> for Level
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<usize> for AtomicUsize
impl From<FuncIndex> for EntityIndex
impl From<GlobalIndex> for EntityIndex
impl From<MemoryIndex> for EntityIndex
impl From<TableIndex> for EntityIndex
impl From<BinaryReaderError> for WasmError
impl From<MemoryType> for Memory
impl From<TagType> for Tag
impl From<KebabString> for String
impl From<LayoutError> for TryReserveErrorKind
impl From<__m128> for Simd<f32, 4>
impl From<__m128d> for Simd<f64, 2>
impl From<__m128i> for Simd<i8, 16>
impl From<__m128i> for Simd<i16, 8>
impl From<__m128i> for Simd<i32, 4>
impl From<__m128i> for Simd<i64, 2>
impl From<__m128i> for Simd<isize, 4>
impl From<__m128i> for Simd<u8, 16>
impl From<__m128i> for Simd<u16, 8>
impl From<__m128i> for Simd<u32, 4>
impl From<__m128i> for Simd<u64, 2>
impl From<__m128i> for Simd<usize, 4>
impl From<__m256> for Simd<f32, 8>
impl From<__m256d> for Simd<f64, 4>
impl From<__m256i> for Simd<i8, 32>
impl From<__m256i> for Simd<i16, 16>
impl From<__m256i> for Simd<i32, 8>
impl From<__m256i> for Simd<i64, 4>
impl From<__m256i> for Simd<isize, 8>
impl From<__m256i> for Simd<u8, 32>
impl From<__m256i> for Simd<u16, 16>
impl From<__m256i> for Simd<u32, 8>
impl From<__m256i> for Simd<u64, 4>
impl From<__m256i> for Simd<usize, 8>
impl From<__m512> for Simd<f32, 16>
impl From<__m512d> for Simd<f64, 8>
impl From<__m512i> for Simd<i8, 64>
impl From<__m512i> for Simd<i16, 32>
impl From<__m512i> for Simd<i32, 16>
impl From<__m512i> for Simd<i64, 8>
impl From<__m512i> for Simd<isize, 16>
impl From<__m512i> for Simd<u8, 64>
impl From<__m512i> for Simd<u16, 32>
impl From<__m512i> for Simd<u32, 16>
impl From<__m512i> for Simd<u64, 8>
impl From<__m512i> for Simd<usize, 16>
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<NonZeroU32> for getrandom::error::Error
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, 4>> for __m128
impl From<Simd<f32, 8>> for __m256
impl From<Simd<f32, 16>> for __m512
impl From<Simd<f64, 2>> for __m128d
impl From<Simd<f64, 4>> for __m256d
impl From<Simd<f64, 8>> for __m512d
impl From<Simd<i8, 16>> for __m128i
impl From<Simd<i8, 32>> for __m256i
impl From<Simd<i8, 64>> for __m512i
impl From<Simd<i16, 8>> for __m128i
impl From<Simd<i16, 16>> for __m256i
impl From<Simd<i16, 32>> for __m512i
impl From<Simd<i32, 4>> for __m128i
impl From<Simd<i32, 8>> for __m256i
impl From<Simd<i32, 16>> for __m512i
impl From<Simd<i64, 2>> for __m128i
impl From<Simd<i64, 4>> for __m256i
impl From<Simd<i64, 8>> for __m512i
impl From<Simd<isize, 4>> for __m128i
impl From<Simd<isize, 8>> for __m256i
impl From<Simd<isize, 16>> for __m512i
impl From<Simd<u8, 16>> for __m128i
impl From<Simd<u8, 32>> for __m256i
impl From<Simd<u8, 64>> for __m512i
impl From<Simd<u16, 8>> for __m128i
impl From<Simd<u16, 16>> for __m256i
impl From<Simd<u16, 32>> for __m512i
impl From<Simd<u32, 4>> for __m128i
impl From<Simd<u32, 8>> for __m256i
impl From<Simd<u32, 16>> for __m512i
impl From<Simd<u64, 2>> for __m128i
impl From<Simd<u64, 4>> for __m256i
impl From<Simd<u64, 8>> for __m512i
impl From<Simd<usize, 4>> for __m128i
impl From<Simd<usize, 8>> for __m256i
impl From<Simd<usize, 16>> for __m512i
impl From<Box<str, Global>> for String
impl From<Box<CStr, Global>> for CString
impl From<Box<OsStr, Global>> for OsString
impl From<Box<Path, Global>> for PathBuf
impl From<CString> for Box<CStr, Global>
impl From<CString> for Rc<CStr>
impl From<CString> for Arc<CStr>
impl From<CString> for Vec<u8, Global>
impl From<NulError> for std::io::error::Error
impl From<Rc<str>> for Rc<[u8]>
impl From<String> for Box<str, Global>
impl From<String> for Box<dyn Error + 'static, Global>
impl From<String> for Box<dyn Error + Send + Sync + 'static, Global>
impl From<String> for Rc<str>
impl From<String> for Arc<str>
impl From<String> for Vec<u8, Global>
impl From<String> for OsString
impl From<String> for PathBuf
impl From<Arc<str>> for Arc<[u8]>
impl From<Vec<NonZeroU8, Global>> for CString
impl From<StreamResult> for Result<MZStatus, MZError>
impl From<OsString> for Box<OsStr, Global>
impl From<OsString> for Rc<OsStr>
impl From<OsString> for Arc<OsStr>
impl From<OsString> for PathBuf
impl From<File> for OwnedFd
impl From<File> for Stdio
impl From<Error> for gimli::read::Error
impl From<TcpListener> for OwnedFd
impl From<TcpStream> for OwnedFd
impl From<UdpSocket> for OwnedFd
impl From<OwnedFd> for File
impl From<OwnedFd> for TcpListener
impl From<OwnedFd> for TcpStream
impl From<OwnedFd> for UdpSocket
impl From<OwnedFd> for PidFd
impl From<OwnedFd> for UnixDatagram
impl From<OwnedFd> for UnixListener
impl From<OwnedFd> for UnixStream
impl From<OwnedFd> for Stdio
impl From<PidFd> for OwnedFd
impl From<UnixDatagram> for OwnedFd
impl From<UnixListener> for OwnedFd
impl From<UnixStream> for OwnedFd
impl From<PathBuf> for Box<Path, Global>
impl From<PathBuf> for Rc<Path>
impl From<PathBuf> for Arc<Path>
impl From<PathBuf> for OsString
impl From<ChildStderr> for OwnedFd
impl From<ChildStderr> for Stdio
impl From<ChildStdin> for OwnedFd
impl From<ChildStdin> for Stdio
impl From<ChildStdout> for OwnedFd
impl From<ChildStdout> for Stdio
impl From<RecvError> for RecvTimeoutError
impl From<RecvError> for TryRecvError
impl From<Error> for Box<dyn Error + 'static, Global>
impl From<Error> for Box<dyn Error + Send + 'static, Global>
impl From<Error> for Box<dyn Error + Send + Sync + 'static, Global>
impl From<Url> for String
String conversion.
impl From<Errors> for Result<(), Errors>
impl From<Errors> for ParseError
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> From<&'a str> for Cow<'a, str>
impl<'a> From<&'a CStr> for Cow<'a, CStr>
impl<'a> From<&'a CString> for Cow<'a, CStr>
impl<'a> From<&'a String> for Cow<'a, str>
impl<'a> From<&'a OsStr> for Cow<'a, OsStr>
impl<'a> From<&'a OsString> for Cow<'a, OsStr>
impl<'a> From<&'a Path> for Cow<'a, Path>
impl<'a> From<&'a PathBuf> for Cow<'a, Path>
impl<'a> From<&str> for Box<dyn Error + Send + Sync + 'a, Global>
impl<'a> From<Cow<'a, str>> for Box<dyn Error + 'static, Global>
impl<'a> From<Cow<'a, str>> for String
impl<'a> From<Cow<'a, CStr>> for CString
impl<'a> From<Cow<'a, OsStr>> for OsString
impl<'a> From<Cow<'a, Path>> for PathBuf
impl<'a> From<CString> for Cow<'a, CStr>
impl<'a> From<String> for Cow<'a, str>
impl<'a> From<OsString> for Cow<'a, OsStr>
impl<'a> From<PathBuf> for Cow<'a, Path>
impl<'a> From<PercentDecode<'a>> for Cow<'a, [u8]>
impl<'a> From<PercentEncode<'a>> for Cow<'a, str>
impl<'a, 'b> From<Cow<'b, str>> for Box<dyn Error + Send + Sync + 'a, Global>
impl<'a, B> From<Cow<'a, B>> for Rc<B>where B: ToOwned + ?Sized, Rc<B>: From<&'a B> + From<<B as ToOwned>::Owned>,
impl<'a, B> From<Cow<'a, B>> for Arc<B>where B: ToOwned + ?Sized, Arc<B>: From<&'a B> + From<<B as ToOwned>::Owned>,
impl<'a, E> From<E> for Box<dyn Error + 'a, Global>where E: Error + 'a,
impl<'a, E> From<E> for Box<dyn Error + Send + Sync + 'a, Global>where E: Error + Send + Sync + 'a,
impl<'a, T> From<&'a Option<T>> for Option<&'a T>
impl<'a, T> From<&'a [T]> for Cow<'a, [T]>where T: Clone,
impl<'a, T> From<&'a Vec<T, Global>> for Cow<'a, [T]>where T: Clone,
impl<'a, T> From<&'a mut Option<T>> for Option<&'a mut T>
impl<'a, T> From<Cow<'a, [T]>> for Vec<T, Global>where [T]: ToOwned<Owned = Vec<T, Global>>,
impl<'a, T> From<Vec<T, Global>> for Cow<'a, [T]>where T: Clone,
impl<'data> From<&'data mut [u8]> for BorrowedBuf<'data>
Create a new BorrowedBuf
from a fully initialized slice.
impl<'data> From<&'data mut [MaybeUninit<u8>]> for BorrowedBuf<'data>
Create a new BorrowedBuf
from an uninitialized buffer.
Use set_init
if part of the buffer is known to be already initialized.
impl<'input, Endian> From<EndianSlice<'input, Endian>> for &'input [u8]where Endian: Endianity,
impl<'s, T> From<&'s mut [T]> for SliceVec<'s, T>
impl<'s, T, A> From<&'s mut A> for SliceVec<'s, T>where A: AsMut<[T]>,
impl<A> From<Box<str, A>> for Box<[u8], A>where A: Allocator,
impl<A> From<A> for ArrayVec<A>where A: Array,
impl<A> From<A> for TinyVec<A>where A: Array,
impl<A> From<ArrayVec<A>> for TinyVec<A>where A: Array,
impl<E> From<Rel32<E>> for Rela32<E>where E: Endian,
impl<E> From<Rel64<E>> for Rela64<E>where E: Endian,
impl<E> From<E> for Report<E>where E: Error,
impl<E> From<E> for anyhow::Errorwhere E: Error + Send + Sync + 'static,
impl<I> From<(I, u16)> for SocketAddrwhere I: Into<IpAddr>,
impl<K, V, A, const N: usize> From<[(K, V); N]> for HashMap<K, V, RandomState, A>where K: Eq + Hash, A: Default + Allocator + Clone,
impl<K, V, const N: usize> From<[(K, V); N]> for BTreeMap<K, V, Global>where K: Ord,
impl<K, V, const N: usize> From<[(K, V); N]> for std::collections::hash::map::HashMap<K, V, RandomState>where K: Eq + Hash,
impl<K, V, const N: usize> From<[(K, V); N]> for IndexMap<K, V, RandomState>where K: Hash + Eq,
impl<P: PtrSize> From<VMOffsetsFields<P>> for VMOffsets<P>
impl<R> From<R> for DebugAbbrev<R>
impl<R> From<R> for DebugAddr<R>
impl<R> From<R> for DebugAranges<R>
impl<R> From<R> for DebugFrame<R>where R: Reader,
impl<R> From<R> for EhFrame<R>where R: Reader,
impl<R> From<R> for EhFrameHdr<R>where R: Reader,
impl<R> From<R> for DebugCuIndex<R>
impl<R> From<R> for DebugTuIndex<R>
impl<R> From<R> for DebugLine<R>
impl<R> From<R> for DebugLoc<R>
impl<R> From<R> for DebugLocLists<R>
impl<R> From<R> for DebugPubNames<R>where R: Reader,
impl<R> From<R> for DebugPubTypes<R>where R: Reader,
impl<R> From<R> for DebugRanges<R>
impl<R> From<R> for DebugRngLists<R>
impl<R> From<R> for DebugLineStr<R>
impl<R> From<R> for DebugStr<R>
impl<R> From<R> for DebugStrOffsets<R>
impl<R> From<R> for DebugInfo<R>
impl<R> From<R> for DebugTypes<R>
impl<T> From<&[T]> for Box<[T], Global>where T: Copy,
impl<T> From<&[T]> for Rc<[T]>where T: Clone,
impl<T> From<&[T]> for Arc<[T]>where T: Clone,
impl<T> From<&[T]> for Vec<T, Global>where T: Clone,
impl<T> From<&mut [T]> for Vec<T, Global>where T: Clone,
impl<T> From<Option<T>> for PackedOption<T>where T: ReservedValue,
impl<T> From<Cow<'_, [T]>> for Box<[T], Global>where T: Copy,
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.