Retrieve file size and modification time in Rust using std::fs::metadata and its len and modified methods.
Use std::fs::metadata to retrieve a Metadata struct containing the file size and modification time.
use std::fs;
fn main() -> std::io::Result<()> {
let metadata = fs::metadata("filename.txt")?;
let size = metadata.len();
let modified = metadata.modified()?;
println!("Size: {size} bytes, Modified: {modified:?}");
Ok(())
}
Getting file metadata in Rust asks the operating system for details about a specific file, like its size and when it was last modified. It's like checking the label on a package to see how heavy it is and when it was shipped. You use this whenever your program needs to know the properties of a file before reading or processing it.