我有一系列元素,需要将其分为12个块,并计算每个块的平均值:
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);
}
我得到一个错误:
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`