【Rust】ファイルの拡張子を取得して、任意のファイル名に変更

正規表現(regex)もあるけど、単純にreplaceを使う。

let tmp_path = "./tmp/test.png";
    let str = &tmp_path.replace("./tmp/", "");
    let pos = str.find('.').unwrap();
    let file_extension = &str[(pos+1)..].to_string();
    let time = Utc::now().format("%Y%m%d%H%M%S").to_string();
    let file_key = format!("{}.{}", time, file_extension);
    println!("{}", file_key);

Finished `dev` profile [unoptimized + debuginfo] target(s) in 7.22s
Running `target/debug/axum`
20250217071013.png