what’s the easiest way to print next week’s days with rust?

With the current date, an offset (remaining for next week) 7 - now.weekday().num_days_from_monday() as i64 + day) can be computed. The rest is just an iteration over the week days.

use chrono::Duration;
use chrono::Datelike;
use chrono::Local;

fn main() {
    let now = Local::now();
    for day in 0..7 {
        println!("{}", (now + Duration::days(7 - now.weekday().num_days_from_monday() as i64 + day)).format("%Y-%m-%d %A"));
        println!("=======================");
    }
}

playground

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top