Skip to content

Commit 942403c

Browse files
committed
Implemented Path::starts_with
1 parent d349333 commit 942403c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/path/path.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,31 @@ impl Path {
566566
fs::read_link(self).await
567567
}
568568

569+
/// Determines whether `base` is a prefix of `self`.
570+
///
571+
/// Only considers whole path components to match.
572+
///
573+
/// # Examples
574+
///
575+
/// ```
576+
/// use async_std::path::Path;
577+
///
578+
/// let path = Path::new("/etc/passwd");
579+
///
580+
/// assert!(path.starts_with("/etc"));
581+
/// assert!(path.starts_with("/etc/"));
582+
/// assert!(path.starts_with("/etc/passwd"));
583+
/// assert!(path.starts_with("/etc/passwd/"));
584+
///
585+
/// assert!(!path.starts_with("/e"));
586+
/// ```
587+
pub fn starts_with<P: AsRef<Path>>(&self, base: P) -> bool
588+
where
589+
P: std::convert::AsRef<std::path::Path>,
590+
{
591+
self.inner.starts_with(base)
592+
}
593+
569594
/// Queries the metadata about a file without following symlinks.
570595
///
571596
/// This is an alias to [`fs::symlink_metadata`].

0 commit comments

Comments
 (0)