Error while calculating Getting average of chunks

I've a series of elements, that I need to divide them into chunks of 12, then calculate the average of each chunk, I wrote the below code, but got error:

error[E0282]: type annotations needed
--> src/main.rs:20:26
|
20 | .map(|chunk| chunk.iter().sum() as f32/ chunk.len() as f32)
| ^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for S

error: aborting due to previous error

PlayGround

fn main() {
    let series = [30,21,29,31,40,48,53,47,37,39,31,29,17,9,20,24,27,35,41,38,
          27,31,27,26,21,13,21,18,33,35,40,36,22,24,21,20,17,14,17,19,
          26,29,40,31,20,24,18,26,17,9,17,21,28,32,46,33,23,28,22,27,
          18,8,17,21,31,34,44,38,31,30,26,32];

    find_chunks_averages(&series, 12);
}

fn find_chunks_averages(series: &[i32], chunk_length: i32){
    let _season_averages = series.to_vec();

    let _chunks = series
            .chunks(chunk_length as usize)
            .collect::<Vec<_>>();

    let _chunks_averages= _chunks
            .iter()
            .map(|chunk| chunk.iter().sum() as f32/ chunk.len() as f32)
            .collect::<Vec<f32>>();

        println!("The chunks you have are:: {:#?}", _chunks);
        println!("The averagesof the chunks are: {:#?}", _chunks_averages);
}

The S come from sum.
You just need to add the qualifier like used in other parts of the code;
.sum::<i32>()

1 Like