Iterator + Borrow Checker: Work around borrowing already borrowed mutable variable

⚓ Rust    📅 2026-05-15    👤 surdeus    👁️ 2      

surdeus

I am working on a program to map data to a canvas. I have a sample vector of structures that contains the data which will be used to calculate the pixel locations on the canvas and placed via a vector of points. With help from this forum I have worked out calculating the x axis pixels from the data with an iterator but am unable to do so for the y axis. I need to calculate the min and max data values then use the min value in a subsequent calculation to determine the pixel values. Unfortunately I have run into the borrow checker which will not let me immutably borrow the variable min_close in the map method because it is mutably borrowed in the inspect method. I do not understand how to work around this issue. Any assistance will be appreciated.

struct ClosingsByDay {
    date: String,
    closing: f32,
}


pub fn line_graph_x_data() -> Vec<f32> {

    let date_closing_vec: Vec<ClosingsByDay> = vec![
        ClosingsByDay { date:  "2026-02-02".to_string(), closing: 310.05},
        ClosingsByDay { date:  "2026-03-14".to_string(), closing: 322.77},
        ClosingsByDay { date:  "2026-03-29".to_string(), closing: 302.29},
        ClosingsByDay { date:  "2026-04-09".to_string(), closing: 336.45},
        ClosingsByDay { date:  "2026-05-22".to_string(), closing: 352.95},
        ClosingsByDay { date:  "2026-07-15".to_string(), closing: 345.59},
    ];

    let starting_date = NaiveDate::parse_from_str(&date_closing_vec[0].date, "%Y-%m-%d").expect("error");

    let mut duration_iter = date_closing_vec
        .iter()
        .filter_map(|item| NaiveDate::parse_from_str(&item.date, "%Y-%m-%d").ok())
        .map(|nav_date| nav_date.signed_duration_since(starting_date).num_days());

    let last_duration = duration_iter.next_back().unwrap();

    let x_axis = duration_iter
        .map(|duration| duration as f32 / last_duration as f32)
        .chain([1.0])
        .map(|ratio| ((ratio * 600.0) + 100.0).round())
        .collect::<Vec<f32>>();

    for item in &x_axis {
        println!("duration = {}", item);
    }

    let mut max_close = date_closing_vec[0].closing;
    let mut min_close = date_closing_vec[0].closing;
    let close_delta = max_close - min_close;

    let y_axis = date_closing_vec
        .iter()
        .inspect(|element| {
            if element.closing > max_close {
                max_close = element.closing;
            }
            if element.closing < min_close {
                min_close = element.closing;
            }
        })
        
        .map(|element| {

             (700.0 - ((element.closing - min_close) / close_delta) + 100.0).round()    
// cannnot borrow min_close as immutable in above line
// already borrowed as mutable                    

        })
        .collect::<Vec<f32>>();        

    for item in &y_axis {
        println!("y value = {}", item);
    }

    x_axis

}

4 posts - 3 participants

Read full topic

🏷️ Rust_feed