How to check if a path is a subdirectory of another path?

Viewed 609

I want to check if a path is a subdirectory of another path:

use std::path::Path;

let path = Path::new("/foo/bar/");
let child = Path::new("/foo/bar/baz");

assert_eq!(is_subdirectory(path, child), true);

How to do this?

1 Answers

Path's method starts_with works

use std::path::Path;

let path = Path::new("/foo/bar/");
let child = Path::new("/foo/bar/baz");

assert_eq!(child.starts_with(path), true);
assert_eq!(path.starts_with(child), false);
Related