#prefix-path #find #set

common-path

Finds the common prefix between a set of paths

1 stable release

Uses old Rust 2015

1.0.0 Nov 15, 2018

#1330 in Filesystem

Download history 64355/week @ 2025-09-14 60839/week @ 2025-09-21 64966/week @ 2025-09-28 74040/week @ 2025-10-05 56004/week @ 2025-10-12 66501/week @ 2025-10-19 62151/week @ 2025-10-26 64557/week @ 2025-11-02 81722/week @ 2025-11-09 68173/week @ 2025-11-16 75260/week @ 2025-11-23 79887/week @ 2025-11-30 76131/week @ 2025-12-07 75224/week @ 2025-12-14 36100/week @ 2025-12-21 34240/week @ 2025-12-28

225,524 downloads per month
Used in 1,096 crates (14 directly)

MIT/Apache

7KB
75 lines

common-path

Documentation

A small crate that provides functions for determining the common prefix, if any, between a set of paths

Installation

In your Cargo.toml, add this to the [dependencies] section:

common-path = "1"

and in your crate root, add

// src/lib.rs, src/main.rs, etc
extern crate common_path;

Usage

There are two functions provided: common_path, and common_path_all

extern crate common_path;
use std::path::Path;

fn main() {
    let a = Path::new("/a/b/c/d");
    let b = Path::new("/a/b/e/f");
    let prefix = common_path::common_path(a, b); // => Some(Path::new("/a/b"))
}

If you need to find a common prefix for more than 2 paths, common_path_all takes anything that can be turned into an iterator of Path references:

extern crate common_path;
use std::path::Path;

fn main() {
    let a = Path::new("/a/b/c/d");
    let b = Path::new("/a/b/e/f");
    let c = Path::new("/a/g/h/i");
    let prefix = common_path::common_path_all(vec![a, b, c]); // => Some(Path::new("/a"))
}

Notes

This library makes no attempt to canonicalize the paths, so 2 paths that should theoretically have a common prefix might get missed unless they are canonicalized beforehand.

For example, /foo/bar/baz and /foo/quux/../bar/baz/quuux should have the common prefix /foo/bar/baz, once they are canonicalized, but in this form, this library will return a prefix of /foo. If you call Path::canonicalize on them beforehand, you will get the "correct" prefix, but canonicalize will return on an error on paths that don't actually exist, so I wanted to avoid using it in this library.

No runtime deps