pub struct Path { /* private fields */ }
Expand description
A slice of a path.
This type is just a wrapper around a str
.
Implementations§
source§impl Path
impl Path
sourcepub fn new<S>(s: &S) -> &Selfwhere
S: AsRef<str> + ?Sized,
pub fn new<S>(s: &S) -> &Selfwhere S: AsRef<str> + ?Sized,
Wraps a string slice as a path slice.
This is a cost-free conversion.
sourcepub fn components(&self) -> Components<'_> ⓘ
pub fn components(&self) -> Components<'_> ⓘ
Produces an iterator over the Component
s of the path.
When parsing the path there is a small amount of normalization:
- Repeated separators are ignored, so
a/b
anda//b
both havea
andb
as components. - Occurrences of
.
are normalized away, except if they are at the beginning of the path. For example,a/./b
,a/b/
,a/b/.
anda/b
all havea
andb
as components, but./a/b
starts with an additionalCurDir
component. - A trailing slash is normalized away,
/a/b
and/a/b/
are equivalent.
Examples
let mut components = Path::new("/tmp/foo.txt").components();
assert_eq!(components.next(), Some(Component::RootDir));
assert_eq!(components.next(), Some(Component::Normal("tmp")));
assert_eq!(components.next(), Some(Component::Normal("foo.txt")));
assert_eq!(components.next(), None)
sourcepub fn is_absolute(&self) -> bool
pub fn is_absolute(&self) -> bool
Returns true if the path starts with the root.
Examples
assert!(Path::new("/foo.txt").is_absolute());
assert!(!Path::new("foo.txt").is_absolute());
sourcepub fn parent(&self) -> Option<&Self>
pub fn parent(&self) -> Option<&Self>
Returns the path without its final component, if there is one.
Examples
let path = Path::new("/foo/bar");
let parent = path.parent().unwrap();
assert_eq!(parent, Path::new("/foo"));
let grand_parent = parent.parent().unwrap();
assert_eq!(grand_parent, Path::new("/"));
assert_eq!(grand_parent.parent(), None);
let relative_path = Path::new("foo/bar");
let parent = relative_path.parent();
assert_eq!(parent, Some(Path::new("foo")));
let grand_parent = parent.and_then(Path::parent);
assert_eq!(grand_parent, Some(Path::new("")));
assert_eq!(grand_parent, Some(Path::new("")));
let great_grand_parent = grand_parent.and_then(Path::parent);
assert_eq!(great_grand_parent, None);
sourcepub fn file_name(&self) -> Option<&str>
pub fn file_name(&self) -> Option<&str>
Returns the final component of the Path
, if there is one.
If the path is a normal file, this is the file name. If it’s the path of a directory, this is the directory name.
Returns None
if the path terminates in ..
.
Examples
assert_eq!(Some("bin"), Path::new("/usr/bin/").file_name());
assert_eq!(Some("foo.txt"), Path::new("tmp/foo.txt").file_name());
assert_eq!(Some("foo.txt"), Path::new("foo.txt/.").file_name());
assert_eq!(Some("foo.txt"), Path::new("foo.txt/.//").file_name());
assert_eq!(None, Path::new("foo.txt/..").file_name());
assert_eq!(None, Path::new("/").file_name());
sourcepub fn file_stem(&self) -> Option<&str>
pub fn file_stem(&self) -> Option<&str>
Extracts the stem (non-extension) portion of self.file_name
.
The stem is:
None
, if there is no file name;- The entire file name if there is no embedded
.
; - The entire file name if the file name begins with
.
and has no other.
s within; - Otherwise, the portion of the file name before the final
.
Examples
assert_eq!("foo", Path::new("foo.rs").file_stem().unwrap());
assert_eq!(".foo", Path::new(".foo").file_stem().unwrap());
assert_eq!("foo.tar", Path::new("foo.tar.gz").file_stem().unwrap());
sourcepub fn get(&self, cwd: &DirRef) -> Option<FileOrDir>
pub fn get(&self, cwd: &DirRef) -> Option<FileOrDir>
Returns the file or directory at the given path.
The path can be relative or absolute.
If the path does not point to a file system object, None
is returned.
sourcepub fn get_file(&self, cwd: &DirRef) -> Option<FileRef>
pub fn get_file(&self, cwd: &DirRef) -> Option<FileRef>
Returns the file at the given path.
The path can be relative or absolute.
If the path does not point to a file, None
is returned.
sourcepub fn get_dir(&self, cwd: &DirRef) -> Option<DirRef>
pub fn get_dir(&self, cwd: &DirRef) -> Option<DirRef>
Returns the directory at the given path.
The path can be relative or absolute.
If the path does not point to a directory, None
is returned.
sourcepub fn get_absolute(path: &Path) -> Option<FileOrDir>
pub fn get_absolute(path: &Path) -> Option<FileOrDir>
Returns the file or directory at the given absolute path.
If the path does not point to a file system object or the path is
relative, None
is returned.
sourcepub fn relative<P>(&self, base: P) -> Option<PathBuf>where
P: AsRef<Path>,
pub fn relative<P>(&self, base: P) -> Option<PathBuf>where P: AsRef<Path>,
Construct a relative path from a provided base directory path to the provided path.
sourcepub fn extension(&self) -> Option<&str>
pub fn extension(&self) -> Option<&str>
Extracts the extension (without the leading dot) of self.file_name
,
if possible.
The extension is:
None
, if there is no file name;None
, if there is no embedded.
;None
, if the file name begins with.
and has no other.
s within;- Otherwise, the portion of the file name after the final
.
Examples
assert_eq!(None, Path::new("foo").extension());
assert_eq!(None, Path::new(".foo").extension());
assert_eq!("rs", Path::new("foo.rs").extension().unwrap());
assert_eq!("gz", Path::new("foo.tar.gz").extension().unwrap());
Trait Implementations§
source§impl PartialEq<Path> for Path
impl PartialEq<Path> for Path
source§impl PartialOrd<Path> for Path
impl PartialOrd<Path> for Path
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more