Iterator + Map - How do divide every element, including the last element, by the last element
⚓ Rust 📅 2026-05-14 👤 surdeus 👁️ 2I am writing a function to convert string dates in a vector to pixel locations on a canvas. I initially achieved this via a series of for loops. But I would really like to be able to do this via an iterator with a series of filter_maps and maps as required. I think I have most of it figured out but in the first map (duration) I cannot figure out how to divide every element in the iterator, including the last element, by the last element. Any assistance will be appreciated.
pub fn line_graph_x_data() -> Result<Vec<f64>, CustomError> {
let date_vec = [
"2026-02-02".to_string(),
"2026-03-14".to_string(),
"2026-03-29".to_string(),
"2026-04-09".to_string(),
"2026-05-22".to_string(),
"2026-07-15".to_string(),
];
// 1) convert strings to naive dates
// 2) Determine number of days between each date and first date ("2026-02-02") in the date_vec
// 3) Determine number of days between top and bottom dates (earliest to most recent)
// 4) Apply formula (((each_value - min_vallue)/(max_value - min_value)) * 600) + 100) and round
// working code below #################
// let mut navidate_vec: Vec<NaiveDate> = Vec::new();
// for i in 0..date_vec.len() {
// let nav_date = NaiveDate::parse_from_str(&date_vec[i], "%Y-%m-%d")?;
// navidate_vec.push(nav_date);
// }
// let mut numerator_vec = Vec::new();
// for i in 0..navidate_vec.len() {
// let day_delta = navidate_vec[i].signed_duration_since(navidate_vec[0]).num_days();
// numerator_vec.push(day_delta);
// }
// let denominator = numerator_vec[numerator_vec.len()-1];
// let mut x_axis_pixels = Vec::new();
// for i in 0..numerator_vec.len() {
// let num_pixels = (((numerator_vec[i] as f64 / denominator as f64)) * 600.0) + 100.0;
// x_axis_pixels.push(num_pixels.round());
// }
// Ok(x_axis_pixels)
// working code above #################
let x_axis = date_vec.iter()
.filter_map(|item| {
let nav_date = NaiveDate::parse_from_str(&item, "%Y-%m-%d").ok();
return nav_date
})
.filter_map(|nav_date| {
let starting_date = NaiveDate::parse_from_str(&date_vec[0], "%Y-%m-%d").ok().expect("error");
let num_days = nav_date.signed_duration_since(starting_date)
.num_days();
return Some(num_days)
})
.map(|duration| {
let ratio = duration as f64 / last duration item as f64 ???????
return ratio
})
.map(|ratio| {
let x_values = ((ratio * 600.0) + 100.0).round();
return x_values
})
.collect::<Vec<f64>>();
}
2 posts - 2 participants
🏷️ Rust_feed