[][src]Function filecmp::cmp

pub fn cmp(
    f1: impl AsRef<Path>,
    f2: impl AsRef<Path>,
    shallow: bool
) -> Result<bool>

Compare two files.


  • f1 -- First file name
  • f2 -- Second file name
  • shallow -- Just check stat signature (do not read the files).

Return value:

  • True if the files are the same, False otherwise.

This function uses a cache for past comparisons and the results, with cache entries invalidated if their stat information changes. The cache may be cleared by calling clear_cache().


use std::env;
use std::io::Write;
use std::fs::File;
use filecmp;

let temp_dir = env::temp_dir();
let foo_path = temp_dir.join("foo.txt");
let bar_path = temp_dir.join("bar.txt");
let baz_path = temp_dir.join("baz.txt");

{ // Create files in temporary directory
    let mut foo = File::create(&foo_path).unwrap();
    let mut bar = File::create(&bar_path).unwrap();
    let mut baz = File::create(&baz_path).unwrap();

    foo.write_all(b"hello filecmp!").unwrap();
    bar.write_all(b"hello filecmp!").unwrap();
    baz.write_all(b"hello world!").unwrap();
} // Close them

let a = filecmp::cmp(&foo_path, &bar_path, true).unwrap();
let b = filecmp::cmp(&foo_path, &baz_path, true).unwrap();
let c = filecmp::cmp(&bar_path, &baz_path, true).unwrap();