Skip to main content

Path

Struct Path 

Source
pub struct Path<'a> { /* private fields */ }
Expand description

Path provides a copy-on-write-style path that is built around UTF8 strings.

Implementations§

Source§

impl<'a> Path<'a>

Source

pub const fn new(s: &'a str) -> Self

Create a new path that borrows the provided string.

Source

pub fn into_owned(self) -> Path<'static>

Convert the path into an owned path.

Source

pub fn into_std(&self) -> &Path

View the path as a std::path::Path.

Source

pub fn as_std_path(&self) -> &Path

Convert the path to a std::path::Path.

Source

pub fn as_os_str(&self) -> &OsStr

Convert the path to an OsStr.

Source

pub fn as_str(&self) -> &str

Convert the path to a str.

Source

pub fn to_str(&self) -> Option<&str>

Convert the path to a str.

Source

pub fn to_string_lossy(&self) -> Cow<'_, str>

Convert the path to a string, losslessly because this type is always UTF-8.

Source

pub fn to_path_buf(&self) -> PathBuf

Convert the path to a std::path::PathBuf.

Source

pub fn is_dir(&self) -> Result<bool>

Is the path a directory?

Source

pub fn is_file(&self) -> Result<bool>

Is the path a file?

Is the path a symbolic link?

Source

pub fn basename(&self) -> Path<'_>

Compute the basename of the path. This is guaranteed to be a non-empty path component (falling back to “.” for paths that end with “/”).

Source

pub fn dirname(&self) -> Path<'_>

Compute the dirname of the path. This is guaranteed to be a non-empty path component (falling back to “.” or “/” for single-component paths).

Source

pub fn exists(&self) -> Result<bool>

True if the path exists.

Source

pub fn try_exists(&self) -> Result<bool>

Return whether the path exists, preserving I/O errors.

Source

pub fn metadata(&self) -> Result<Metadata>

Query the filesystem for metadata, following symbolic links.

Query the filesystem for metadata without following symbolic links.

Read the target of a symbolic link.

Source

pub fn read_dir(&self) -> Result<ReadDir>

Return an iterator over the entries in this directory.

Source

pub fn has_root(&self) -> bool

True if the path begins with some number of slashes, other than the POSIX-exception of //.

Source

pub fn has_app_defined(&self) -> bool

True if the path begins with //, but not ///.

Source

pub fn is_abs(&self) -> bool

True if the path is absolute.

Source

pub fn is_absolute(&self) -> bool

True if the path is absolute.

Source

pub fn is_relative(&self) -> bool

True if the path is relative.

Source

pub fn is_normal(&self) -> bool

True if the path contains no “.” components; and, is absolute and has no “..” components, or is relative and has all “..” components at the start.

Source

pub fn join<'b, 'c>(&self, with: impl Into<Path<'b>>) -> Path<'c>
where 'a: 'c, 'b: 'c,

Join to this path another path. Follows standard path rules where if the joined-with path is absolute, the first path is discarded.

Source

pub fn strip_prefix<'b>(&self, prefix: impl Into<Path<'b>>) -> Option<Path<'_>>

Strip a prefix from the path. The prefix and path are allowed to be non-normal and will have “.” components dropped from consideration.

Source

pub fn starts_with<'b>(&self, base: impl Into<Path<'b>>) -> bool

True if the path starts with the provided base path.

Source

pub fn ends_with<'b>(&self, child: impl Into<Path<'b>>) -> bool

True if the path ends with the provided child path.

Source

pub fn parent(&self) -> Option<Path<'_>>

Return the path without its final component, if any.

Source

pub fn ancestors(&self) -> impl Iterator<Item = Path<'static>> + '_

Iterate over the path and its parents.

Source

pub fn file_name(&self) -> Option<&str>

Return the final path component, if it is a normal file name.

Source

pub fn file_stem(&self) -> Option<&str>

Return the file name without its final extension.

Source

pub fn file_prefix(&self) -> Option<&str>

Return the file name without its first extension.

Source

pub fn extension(&self) -> Option<&str>

Return the final file extension without the leading dot.

Source

pub fn with_file_name(&self, file_name: impl AsRef<str>) -> Path<'static>

Create a path like this one, but with its file name replaced.

Source

pub fn with_extension(&self, extension: impl AsRef<str>) -> Path<'static>

Create a path like this one, but with its extension replaced.

Source

pub fn with_added_extension(&self, extension: impl AsRef<str>) -> Path<'static>

Create a path like this one, but with an extension added.

Source

pub fn split(&self) -> (Path<'_>, Path<'_>)

Split the path into basename and dirname components.

Source

pub fn components(&self) -> impl Iterator<Item = Component<'_>>

Return an iterator over the path components. A path with a basename of “.” will always end with Component::CurDir.

Source

pub fn iter(&self) -> impl Iterator<Item = &str>

Iterate over the path components as strings.

Source

pub fn display(&self) -> impl Display + '_

Return a display adapter for the path.

Source

pub fn cwd() -> Option<Path<'a>>

Return the current working directory, if it can be fetched and converted to unicode without error.

Source

pub fn canonicalize(&self) -> Result<Path<'static>, Error>

Return the canonicalized path with all intermediate components normalized and symbolic links resolved.

Trait Implementations§

Source§

impl AsRef<OsStr> for Path<'_>

Source§

fn as_ref(&self) -> &OsStr

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<Path> for Path<'_>

Source§

fn as_ref(&self) -> &Path

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Borrow<Path> for Path<'_>

Source§

fn borrow(&self) -> &Path

Immutably borrows from an owned value. Read more
Source§

impl<'a> Clone for Path<'a>

Source§

fn clone(&self) -> Path<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Path<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Display for Path<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'a> Eq for Path<'a>

Source§

impl<'a> From<&'a Path<'a>> for &'a str

Source§

fn from(path: &'a Path<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a String> for Path<'a>

Source§

fn from(s: &'a String) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a str> for Path<'a>

Source§

fn from(s: &'a str) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Path<'a>> for String

Source§

fn from(path: Path<'a>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Path<'a>> for PathBuf

Source§

fn from(path: Path<'a>) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Path<'_>

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl<'a> Hash for Path<'a>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a> Ord for Path<'a>

Source§

fn cmp(&self, other: &Path<'a>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'a> PartialEq for Path<'a>

Source§

fn eq(&self, other: &Path<'a>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<'a> PartialOrd for Path<'a>

Source§

fn partial_cmp(&self, other: &Path<'a>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a> StructuralPartialEq for Path<'a>

Source§

impl<'a> TryFrom<&'a Path> for Path<'a>

Source§

type Error = Utf8Error

The type returned in the event of a conversion error.
Source§

fn try_from(p: &'a Path) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<OsString> for Path<'_>

Source§

type Error = Utf8Error

The type returned in the event of a conversion error.
Source§

fn try_from(p: OsString) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<PathBuf> for Path<'_>

Source§

type Error = Utf8Error

The type returned in the event of a conversion error.
Source§

fn try_from(p: PathBuf) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

§

impl<'a> Freeze for Path<'a>

§

impl<'a> RefUnwindSafe for Path<'a>

§

impl<'a> Send for Path<'a>

§

impl<'a> Sync for Path<'a>

§

impl<'a> Unpin for Path<'a>

§

impl<'a> UnsafeUnpin for Path<'a>

§

impl<'a> UnwindSafe for Path<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.