I have this script:
use chrono::{NaiveDateTime};
fn func(fmt: &str) -> Result<(), String>{
let date_str = "2020-01-01 00:00";
let dt = NaiveDateTime::parse_from_str(date_str, "%Y-%m-%d %H:%M").unwrap();
let formatted = format!("{}", dt.format(fmt));
println!("formatted: {formatted}");
Ok(())
}
fn main() {
match func("%Y") {
Err(_) => println!("errored"),
Ok(_) => println!("worked"),
}
}
It runs fine, but if I change it to func("%z")
, then format!("{}", dt.format(fmt))
panics.
How can I "catch" this panic, and raise an informative error message if a "wrong" fmt
was passed?