I hope I can use .into() to convert the value to a context where type inference is not possible. This is usually when I want to convert a temporary value to some other type to pass it into a universal function. See the following code for an example ( playground ):
use std::convert::*; struct NewType(pub i32); impl From<NewType> for i32 { fn from(src: NewType) -> i32 { src.0 } } fn main() { let a = NewType(5); println!("{}", a.into());
I get an error:
error[E0282]: type annotations needed --> src/main.rs:13:20 | 13 | println!("{}", a.into()); | ^^^^^^^^ cannot infer type for 'T'
How to tell the compiler that I want to convert to a i32 ?
I can make it work correctly, explicitly Into::<i32>::into(a) Into with arguments like: Into::<i32>::into(a) . This is more verbose and explicit than I had hoped to achieve, especially in a context where I did not import Into ( std::convert::Into::<i32>::into(a) ). a.into::<i32>() would be acceptable, but this is not the place where type arguments go.
a.into() as i32 will look good, but this exact syntax does not work.
Is there a trick I'm missing out on?
source share