// Created on savesnippets.com · https://savesnippets.com/xbvIQ1ZfV5vr7T fn main() { let v = vec![1, 2, 3, 4, 5, 6, 7]; // windows(3) — overlapping for w in v.windows(3) { println!("window: {w:?}"); } // [1, 2, 3] // [2, 3, 4] // [3, 4, 5] ... // chunks(3) — disjoint for c in v.chunks(3) { println!("chunk: {c:?}"); } // [1, 2, 3] // [4, 5, 6] // [7] // Moving average via windows let readings = vec![10.0, 12.0, 11.0, 15.0, 14.0, 18.0]; let avgs: Vec = readings.windows(3) .map(|w| w.iter().sum::() / 3.0) .collect(); println!("3-pt avg: {:?}", avgs); // [11.0, 12.67, 13.33, 15.67] }