'Update File Modification Time

I'm trying to update the file modification metadata of a file using Rust ( a language to which I am very new )

I can access the Metadata as shown here;

https://doc.rust-lang.org/stable/std/fs/struct.Metadata.html#method.modified

 use std::fs;

fn main() -> std::io::Result<()> {
    let metadata = fs::metadata("foo.txt")?;

    if let Ok(time) = metadata.modified() {
        println!("{:?}", time);
    } else {
        println!("Not supported on this platform");
    }
    Ok(())
}

I don't know how to alter that value though. My instinct was to open existing files in append mode and write an empty string--didn't work.

Wondering what a general approach for this would look like?



Solution 1:[1]

You will need a external crate: filetime

Solution 2:[2]

The set_file_mtime function from the filetime crate can update the file modification time metadata:

use filetime::{set_file_mtime, FileTime};

fn main() {
    set_file_mtime("foo.txt", FileTime::now()).unwrap();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cecilio Pardo
Solution 2 Jonathan Feenstra