Why does this code require a type annotation?

I don't understand why I can't convert u16 into usize when subtracting here:

fn main() {
    let n_u16: u16 = 20;
    let n_usize: usize = 20;
    println!("{}", n_usize == n_u16.into()); // works
    println!("{}", n_usize - n_u16.into()); // error[E0284]: type annotations needed
}

Rust playground link: Rust Playground

Honestly it'd be better if neither worked. Because any time this works, it's very fragile to addition of new impls. IIRC that subtraction used to work, but then the new Sub impl for references was added, and it broke a bunch of code.

1 Like

The implementations for references have existed since 1.0.0, but a little experimentation shows they are what stumps inference.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.