Trait into_ext::IntoExt[][src]

pub trait IntoExt<T_>: Into<T_> {
    fn into_<T>(self) -> T
        T: TypeIsEqual<To = T_>
, { ... } }
Expand description

Extension trait for the Into trait, offering a method .into_::<T>() to specify the target type of conversion.

See also TryIntoExt for an extension trait of TryInto, the fallible alternative to Into.

Provided methods

Calling foo.into() using the standard library’s Into trait can lead to ambiguities. Some current workarounds to specify the target type T are to use T::from(foo), or Into::<T>::into(foo). Both of these alternatives are interfering badly with postfix method syntax; the former also doesn’t support types that have a S: Into<T>> but no T: From<S> implementation.

With IntoExt, you can simply write foo.into_::<T>().

The underscore at the end of the method name is to avoid collision with Into::into, and to indicate that the method is followed by additional information (i.e. a type parameter).


use into_ext::IntoExt;

// here’s a big `u32` value, called ‘x’
let x: u32 = u32::MAX;

// now, let’s get x + 10 as a `u64` (without using the `as` operator)
let y = x.into_::<u64>() + 10;

whereas, e.g. the following wouldn’t have worked

let x: u32 = u32::MAX;
let y: u32 = x.into() + 10_u32;