Trait nom::lib::std::convert::Into 1.0.0[−][src]
A value-to-value conversion that consumes the input value. The
opposite of From
.
One should avoid implementing Into
and implement From
instead.
Implementing From
automatically provides one with an implementation of Into
thanks to the blanket implementation in the standard library.
Prefer using Into
over From
when specifying trait bounds on a generic function
to ensure that types that only implement Into
can be used as well.
Note: This trait must not fail. If the conversion can fail, use TryInto
.
Generic Implementations
From
<T> for U
impliesInto<U> for T
Into
is reflexive, which means thatInto<T> for T
is implemented
Implementing Into
for conversions to external types in old versions of Rust
Prior to Rust 1.41, if the destination type was not part of the current crate
then you couldn’t implement From
directly.
For example, take this code:
struct Wrapper<T>(Vec<T>); impl<T> From<Wrapper<T>> for Vec<T> { fn from(w: Wrapper<T>) -> Vec<T> { w.0 } }
This will fail to compile in older versions of the language because Rust’s orphaning rules
used to be a little bit more strict. To bypass this, you could implement Into
directly:
struct Wrapper<T>(Vec<T>); impl<T> Into<Vec<T>> for Wrapper<T> { fn into(self) -> Vec<T> { self.0 } }
It is important to understand that Into
does not provide a From
implementation
(as From
does with Into
). Therefore, you should always try to implement From
and then fall back to Into
if From
can’t be implemented.
Examples
String
implements Into
<
Vec
<
u8
>>
:
In order to express that we want a generic function to take all arguments that can be
converted to a specified type T
, we can use a trait bound of Into
<T>
.
For example: The function is_hello
takes all arguments that can be converted into a
Vec
<
u8
>
.
fn is_hello<T: Into<Vec<u8>>>(s: T) { let bytes = b"hello".to_vec(); assert_eq!(bytes, s.into()); } let s = "hello".to_string(); is_hello(s);
Required methods
Implementors
impl<T, U> Into<U> for T where
U: From<T>,
[src][+]
U: From<T>,
impl Into<Vec<BacktraceFrame, Global>> for Backtrace
impl Into<Vec<BacktraceFrame, Global>> for Backtrace
impl<L, R> Into<Result<R, L>> for Either<L, R>
impl<L, R> Into<Result<R, L>> for Either<L, R>
impl<A, B> Into<Option<Either<A, B>>> for EitherOrBoth<A, B>
impl<A, B> Into<Option<Either<A, B>>> for EitherOrBoth<A, B>
impl Into<ucred> for UnixCredentials
impl Into<ucred> for UnixCredentials
impl Into<i32> for ClockId
impl Into<i32> for ClockId
impl<T> Into<(T, T)> for Ratio<T>
impl<T> Into<(T, T)> for Ratio<T>
impl Into<f32> for OrderedFloat<f32>
impl Into<f32> for OrderedFloat<f32>
impl Into<f64> for OrderedFloat<f64>
impl Into<f64> for OrderedFloat<f64>
impl Into<Error> for FloatIsNan
impl Into<Error> for FloatIsNan
impl<'a> Into<&'a [u32; 4]> for &'a vec128_storage
impl<'a> Into<&'a [u32; 4]> for &'a vec128_storage
impl Into<[u32; 4]> for vec128_storage
impl Into<[u32; 4]> for vec128_storage
impl Into<[u64; 2]> for vec128_storage
impl Into<[u64; 2]> for vec128_storage
impl Into<[u128; 1]> for vec128_storage
impl Into<[u128; 1]> for vec128_storage
impl Into<[u32; 8]> for vec256_storage
impl Into<[u32; 8]> for vec256_storage
impl Into<[u64; 4]> for vec256_storage
impl Into<[u64; 4]> for vec256_storage
impl Into<[u128; 2]> for vec256_storage
impl Into<[u128; 2]> for vec256_storage
impl Into<[u32; 16]> for vec512_storage
impl Into<[u32; 16]> for vec512_storage
impl Into<[u64; 8]> for vec512_storage
impl Into<[u64; 8]> for vec512_storage
impl Into<[u128; 4]> for vec512_storage
impl Into<[u128; 4]> for vec512_storage
impl<T> Into<Vec<T, Global>> for RepeatedField<T>
impl<T> Into<Vec<T, Global>> for RepeatedField<T>
impl<T> Into<[T; 3]> for RGB<T>
impl<T> Into<[T; 3]> for RGB<T>
impl<T> Into<[T; 4]> for RGBA<T>
impl<T> Into<[T; 4]> for RGBA<T>
impl<T> Into<[T; 3]> for BGR<T>
impl<T> Into<[T; 3]> for BGR<T>
impl<T> Into<[T; 4]> for BGRA<T>
impl<T> Into<[T; 4]> for BGRA<T>
impl<T> Into<(T, T, T)> for RGB<T>
impl<T> Into<(T, T, T)> for RGB<T>
impl<T, A> Into<(T, T, T, A)> for RGBA<T, A>
impl<T, A> Into<(T, T, T, A)> for RGBA<T, A>
impl<T> Into<(T, T, T)> for BGR<T>
impl<T> Into<(T, T, T)> for BGR<T>
impl<T, A> Into<(T, T, T, A)> for BGRA<T, A>
impl<T, A> Into<(T, T, T, A)> for BGRA<T, A>
impl Into<String> for Name<'_>
impl Into<String> for Name<'_>
impl<'a> Into<Option<&'a Id>> for &'a Span
impl<'a> Into<Option<&'a Id>> for &'a Span
impl<'a> Into<Option<Id>> for &'a Span
impl<'a> Into<Option<Id>> for &'a Span
impl Into<Option<Id>> for Span
impl Into<Option<Id>> for Span
impl Into<Option<Level>> for LevelFilter
impl Into<Option<Level>> for LevelFilter
impl<'a> Into<Option<Id>> for &'a Id
impl<'a> Into<Option<Id>> for &'a Id
impl<'a> Into<Option<&'a Id>> for &'a Current
impl<'a> Into<Option<&'a Id>> for &'a Current
impl<'a> Into<Option<Id>> for &'a Current
impl<'a> Into<Option<Id>> for &'a Current
impl Into<Option<Id>> for Current
impl Into<Option<Id>> for Current
impl<'a> Into<Option<&'static Metadata<'static>>> for &'a Current
impl<'a> Into<Option<&'static Metadata<'static>>> for &'a Current
impl<'a> Into<&'a str> for UniCase<&'a str>
impl<'a> Into<&'a str> for UniCase<&'a str>
impl<'a> Into<String> for UniCase<String>
impl<'a> Into<String> for UniCase<String>
impl<'a> Into<Cow<'a, str>> for UniCase<Cow<'a, str>>
impl<'a> Into<Cow<'a, str>> for UniCase<Cow<'a, str>>
impl Into<u8> for Level
impl Into<u8> for Level