Trait into_ext::TryIntoExt[][src]

pub trait TryIntoExt<T_>: TryInto<T_> {
    fn try_into_<T>(self) -> Result<T, Self::Error>
    where
        T: TypeIsEqual<To = T_>
, { ... } }
Expand description

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

See also IntoExt for an extension trait of Into, the infallible alternative to TryInto.

Provided methods

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

With TryIntoExt, you can simply write foo.try_into_::<T>().

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

Example

use into_ext::TryIntoExt;

// here’s a small `u64` value, called ‘x’
let x: u64 = 1;

// now, let’s get x - 10 as a `i32` (without using the `as` operator)
let y = x.try_into_::<i32>()? - 10;

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

use core::convert::TryInto;

let x: u64 = 1;
let y: i32 = x.try_into()? - 10_i32;

Implementors