docs

std::path

本頁內容

API 參考只有英文版,由標準函式庫原始碼中的英文文件註解產生。

Path, a UTF-8 path value with Python-style / joining. Separators follow the target: / on Unix; \ or / on Windows, where a path may start with a prefix (C:, \\server\share, \\?\.., \\.\..), is absolute only with a prefix and a root, and join writes \.

Path is immutable: / returns a new one, p /= q is p = p / q. RawPath holds the OS form (Unix bytes, Windows UTF-16); the fs layer never produces one - it reports non-UTF-8 names as InvalidData.

Structs

PathSource

pub struct Path

A filesystem path, held as text. Not checked against the filesystem until an fs call uses it.

  • pub const fn new(s: str) -> Path§

    Source

    The path s spells.

  • pub const fn empty() -> Path§

    Source

    The empty path.

  • pub const fn from_components<I: IntoIterator<Item = str>>(components: I) -> Path§

    Source

    The path joining each of components in turn (Path::join).

  • pub const fn as_str(self) -> str§

    Source

    The path as text.

  • pub const fn to_raw(self) -> RawPath§

    Source

    The path as OS bytes (Unix) or WTF-16 units (Windows).

  • pub const fn is_absolute(self) -> bool§

    Source

    Unix: rooted. Windows: a prefix and a root (C:\x, \\srv\s\x).

  • pub const fn is_relative(self) -> bool§

    Source

    true when the path is not absolute.

  • pub const fn has_root(self) -> bool§

    Source

    true when the path starts at a root (/, \, C:\).

  • pub const fn parent(self) -> Path | none§

    Source

    The path without its last component; none for / and "". Path::new("a").parent() is "" (as in Rust).

  • pub const fn file_name(self) -> str | none§

    Source

    The last component unless it is .. / /.

  • pub const fn file_stem(self) -> str | none§

    Source

    file_name without its extension.

  • pub const fn extension(self) -> str | none§

    Source

    After the last . of file_name (none for .bashrc, a).

  • pub const fn components(self) -> Components§

    Source

    The components, front to back: repeated separators and inner . dropped, a leading . and every .. kept.

  • pub const fn ancestors(self) -> Ancestors§

    Source

    self, its parent, its grandparent, ...

  • pub const fn join<P: AsPath>(self, other: P) -> Path§

    Source

    other (a Path or a str) appended; an absolute other replaces self. Windows: so does one with a prefix (D:x), and a rooted one without (\x) keeps self's prefix.

  • pub const fn with_file_name(self, name: str) -> Path§

    Source

    self with its last component replaced by name.

  • pub const fn with_extension(self, ext: str) -> Path§

    Source

    self with its extension replaced by ext; an empty ext removes it.

  • pub const fn starts_with<P: AsPath>(self, base: P) -> bool§

    Source

    Component-wise prefix test.

  • pub const fn ends_with<P: AsPath>(self, suffix: P) -> bool§

    Source

    Component-wise suffix test.

  • pub const fn strip_prefix<P: AsPath>(self, base: P) -> Path | none§

    Source

    The rest of self after base (component-wise), or none when self does not start with it (str::strip_prefix alike).

  • pub const fn normalize(self) -> Path§

    Source

    Lexical . / .. resolution (no filesystem access).

  • pub fn exists(self) -> bool§

    Source

    Whether the path exists (symlinks followed); any error reads as false.

  • pub fn is_file(self) -> bool§

    Source

    Whether the path is a regular file (symlinks followed); false on any error.

  • pub fn is_dir(self) -> bool§

    Source

    Whether the path is a directory (symlinks followed); false on any error.

  • Source

    Whether the path itself is a symlink; false on any error.

  • pub fn metadata(self) throws (IoError) -> Metadata§

    Source

    The path's metadata, symlinks followed (fs::metadata).

  • pub fn canonicalize(self) throws (IoError) -> Path§

    Source

    Absolute, symlinks resolved (realpath).

Trait implementations 7

RawPathSource

pub struct RawPath

OS path bytes that need not be UTF-8: Unix bytes.

RawPathSource

pub struct RawPath

An OS path that need not be valid UTF-16: Windows WTF-16 units (lone surrogates allowed).

  • pub const fn from_bytes<B: AsBytes>(bytes: B) -> RawPath§

    Source

    The path with OS bytes bytes.

  • pub const fn as_bytes(self) -> Slice<u8>§

    Source

    The OS bytes.

  • pub const fn to_path(self) throws (Utf8Error) -> Path§

    Source

    The path; throws if the bytes are not UTF-8.

  • pub const fn to_path_lossy(self) -> Path§

    Source

    The path with each maximal ill-formed subsequence replaced by one U+FFFD (str::from_utf8_lossy; Windows: each lone surrogate).

  • pub fn from_wide(wide: Slice<u16>) -> RawPath§

    Source

    The path of the UTF-16 units wide, which need not be valid.

  • pub fn as_wide(self) -> Slice<u16>§

    Source

    The UTF-16 units of the path.

  • pub fn to_path(self) throws (Utf8Error) -> Path§

    Source

    The path; throws if the units are not valid UTF-16 (valid_up_to counts UTF-16 units here).

  • pub fn to_path_lossy(self) -> Path§

    Source

    The path with each lone surrogate replaced by U+FFFD.

Trait implementations 1

ComponentsSource

pub struct Components

Path::components: the parts, from the root or first name.

Trait implementations 2

AncestorsSource

pub struct Ancestors

Path::ancestors: the path, then each parent up to the root.

Trait implementations 2

Enums

ComponentSource

pub enum Component

One piece of a path (Path::components).

Trait implementations 1

Traits

AsPathSource

pub trait AsPath

What the fs functions take: a Path or a str spelling one.