Working with `sum` and `reciprocal`

The below code, working fine, and give correct output of: 0.018181818

fn main() {
    let v : i32 = vec![1,2,3,4,5].iter().map(|&x: &i32| x.pow(2)).sum();
    println!("{}",(v as f32).recip());
}

But when I tried to join them in single line as below I failed as the type of output after sum different than required inout type of reciep:

fn main() {
    let v : i32 = vec![1,2,3,4,5].iter().map(|&x: &i32| x.pow(2)).sum().recip();
    println!("{}", v) ;
}

Try:

let v = ([1,2,3,4,5].iter().map(|&x: &i32| x.pow(2)).sum::<i32>() as f32).recip();

1 Like

Thanks a lot :blush:

This question was cross posted to Stack Overflow