# Function pathdiff::diff_paths

source · [−]

    pub fn diff_paths<P, B>(path: P, base: B) -> Option<PathBuf> where
        P: AsRef<Path>,
        B: AsRef<Path>,

Expand description

Construct a relative path from a provided base directory path to the provided path.

    use pathdiff::diff_paths;
    use std::path::*;

    let baz = "/foo/bar/baz";
    let bar = "/foo/bar";
    let quux = "/foo/bar/quux";
    assert_eq!(diff_paths(bar, baz), Some("../".into()));
    assert_eq!(diff_paths(baz, bar), Some("baz".into()));
    assert_eq!(diff_paths(quux, baz), Some("../quux".into()));
    assert_eq!(diff_paths(baz, quux), Some("../baz".into()));
    assert_eq!(diff_paths(bar, quux), Some("../".into()));

    assert_eq!(diff_paths(&baz, &bar.to_string()), Some("baz".into()));
    assert_eq!(diff_paths(Path::new(baz), Path::new(bar).to_path_buf()), Some("baz".into()));