I want to write a function which returns the square of a sum. My first shot looked like this:
fn square_of_sum(lim: u64) -> u64 {
let sum: u64 = (1..lim).sum().pow(2);
sum
}
Which gave me an error:
error[E0282]: type annotations needed
--> src/lib.rs:2:20
|
2 | let sum: u64 = (1..lim).sum().pow(2);
| ^^^^^^^^^^^^^^ cannot infer type for `S`
|
= note: type must be known at this point
So thinking rather C-ish, I explicitly cast the result of sum
to u64
, which still has an error
error[E0282]: type annotations needed
--> src/lib.rs:2:20
|
2 | let sum: u64 = ((1..lim).sum() as u64).pow(2);
| ^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for `S`
|
= note: type must be known at this point
Isn't the result of sum
on a Range<u64>
a u64
? Why is the type still missing after the use of as
?