Your question is Code Analysis and Bug Finding. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
Oxa builds autonomous-driving software, and this function is part of a module that turns a vehicle's recent speed samples into a braking-distance report for the safety monitor.
fn braking_distance_report(speeds_mps: &Vec<i32>, deceleration: i32) -> Vec<i32> {
let mut distances = Vec::new();
for i in 0..speeds_mps.len() + 1 {
let speed = speeds_mps[i];
let delta = speeds_mps[i] - speeds_mps[i - 1];
let distance = (speed * speed) / (2 * deceleration);
distances.push(distance + delta);
}
let peak = speeds_mps.iter().max().unwrap();
let average = speeds_mps.iter().sum::<i32>() / speeds_mps.len() as i32;
println!("peak: {}, average: {}", peak, average);
distances
}
Analyze this function and tell me some bugs that you see, and explain what the code is trying to do. Be specific about which line panics or produces a wrong result, and under what input.