Why I have to be explicit on the type on let big: BigInt = big_b << 64;?

In this code:


fn s() -> u64{
    let big_b = BigInt::from(3);
    let big = big_b << 64;
    big.to_u64_digits().1[0]
}

I was getting:

   |
13 |     let big = big_b << 64;
   |         --- consider giving `big` a type

so I modified to


fn s() -> u64{
    let big_b = BigInt::from(3);
    let big: BigInt = big_b << 64;
    big.to_u64_digits().1[0]
}

and it works.

However, why do I have to give an explicit type? Doesn't Shl's implementation for BigInt return a BigInt always? num::BigInt - Rust

I know that when rustc has to go find a method, it really wants to know what the type is without ambiguity. And since there are multiple possibilities for which Shl implementation is at play (different {integer} types), it bails in this case when it needs to know which to_u64_digits to call.

But I don't actually know the answer to your question, i.e., why it doesn't try to unify all the possible output types. I could make some wild guesses, but that's all they'd be.

You can also do this:

let big = big_b << 64u8; // or whichever {integer}

As it will be able to figure out which implementation it is and thus the type of big.

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.