0

I have a series of elements that I need to divide into chunks of 12 and calculate the average of each chunk:

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);
}

(playground)

I get an error:

error[E0282]: type annotations needed
  --> src/main.rs:19:22
   |
19 |         .map(|chunk| chunk.iter().sum() as f32 / chunk.len() as f32)
   |                      ^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for `S`
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Hasan A Yousef
  • 22,789
  • 24
  • 132
  • 203

0 Answers0