Format NaiveDate from vector correctly

⚓ Rust    📅 2025-09-07    👤 surdeus    👁️ 2      

surdeus

My code:

use chrono::NaiveDate;

fn main() {
 
 let combo_res :(&str, [Vec<NaiveDate>; 3])  = f02();
 println!("combo_res = {:?}", &combo_res);
 
 let d1_len = combo_res.1[0].len();
 println!("d1_len = {:?}", d1_len);

 let mut d1 :Vec<&str> = vec!["x"; d1_len];
 println!("d1 = {:?}\n {:?} ^ {:?}\n", &d1, &d1.len(), &d1.capacity());       
 
 
 for i in 0..d1_len {
    // println!("combo_res.1[0][i] = {:?}", &combo_res.1[0][i]);
    d1[i] = &combo_res.1[0][i].format("%d-%m-%Y").to_string();
 }
 
 // [1] want to get: ["01-01-1971", "02-01-1971", "03-01-1971"]    
 println!("d1 = {:?}", &d1);  // Error: borrow later used here.
 // then also for `d2` want to get: ["04-02-1972", "05-02-1972"]
}

fn f02() -> (&'static str, [Vec<NaiveDate>; 3]) {
    let d :(&str, [Vec<NaiveDate>; 3]) =
    ("abc", 
    [
    vec![
    NaiveDate::from_ymd_opt(1971, 1, 1).unwrap(), 
    NaiveDate::from_ymd_opt(1971, 1, 2).unwrap(), 
    NaiveDate::from_ymd_opt(1971, 1, 3).unwrap()
    ],
    vec![
    NaiveDate::from_ymd_opt(1972, 2, 4).unwrap(), 
    NaiveDate::from_ymd_opt(1972, 2, 5).unwrap(), 
    ],
    vec![
    NaiveDate::from_ymd_opt(1973, 3, 6).unwrap(), 
    NaiveDate::from_ymd_opt(1973, 3, 7).unwrap(), 
    NaiveDate::from_ymd_opt(1973, 3, 8).unwrap()
    ]
    ]
    );
    d
}

How to get

[1] want to get: ["01-01-1971", "02-01-1971", "03-01-1971"]

correctly for speed, without additional variables?

1 post - 1 participant

Read full topic

🏷️ Rust_feed