Struct std::path::Path 1.0.0[−][src]
pub struct Path { /* fields omitted */ }
Expand description
路径的切片 (类似于 str
)。
此类型支持许多检查路径的操作,包括将路径分为其各个组成部分 (由 Unix 上的 /
和 Windows 上的 /
或 \
分隔),提取文件名,确定路径是否为绝对路径,等等。。
这是未定义大小的类型,表示必须始终在 &
或 Box
之类的指针后面使用它。
有关此类型的拥有版本,请参见 PathBuf
。
有关整体方法的更多详细信息,请参见 模块级文档。
Examples
use std::path::Path;
use std::ffi::OsStr;
// Note: 这个例子可以在 Windows 上使用
let path = Path::new("./foo/bar.txt");
let parent = path.parent();
assert_eq!(parent, Some(Path::new("./foo")));
let file_stem = path.file_stem();
assert_eq!(file_stem, Some(OsStr::new("bar")));
let extension = path.extension();
assert_eq!(extension, Some(OsStr::new("txt")));
RunImplementations
将 Path
转换为 Cow<str>
。
任何非 Unicode 序列都将替换为 U+FFFD REPLACEMENT CHARACTER
。
Examples
使用有效的 Unicode 在 Path
上调用 to_string_lossy
:
use std::path::Path;
let path = Path::new("foo.txt");
assert_eq!(path.to_string_lossy(), "foo.txt");
Run如果 path
包含无效的 unicode,则 to_string_lossy
调用可能已返回 "fo.txt"
。
如果 Path
是相对的,即不是绝对的,则返回 true
。
有关更多详细信息,请参见 is_absolute
的文档。
Examples
use std::path::Path;
assert!(Path::new("foo.txt").is_relative());
Run如果没有 Path
,则返回不包含其最终组成部分的 Path
。
如果路径以根或前缀结尾,则返回 None
。
Examples
use std::path::Path;
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);
Run在 Path
及其祖先上生成一个迭代器。
如果 parent
方法使用了 0 次或多次,则迭代器将产生返回的 Path
。
这意味着,迭代器将产生 &self
,&self.parent().unwrap()
,&self.parent().unwrap().parent().unwrap()
等。如果 parent
方法返回 None
,则迭代器也将这样做。
迭代器将始终产生至少一个值,即 &self
。
Examples
use std::path::Path;
let mut ancestors = Path::new("/foo/bar").ancestors();
assert_eq!(ancestors.next(), Some(Path::new("/foo/bar")));
assert_eq!(ancestors.next(), Some(Path::new("/foo")));
assert_eq!(ancestors.next(), Some(Path::new("/")));
assert_eq!(ancestors.next(), None);
let mut ancestors = Path::new("../foo/bar").ancestors();
assert_eq!(ancestors.next(), Some(Path::new("../foo/bar")));
assert_eq!(ancestors.next(), Some(Path::new("../foo")));
assert_eq!(ancestors.next(), Some(Path::new("..")));
assert_eq!(ancestors.next(), Some(Path::new("")));
assert_eq!(ancestors.next(), None);
Run返回 Path
的最后一个组件 (如果有)。
如果路径是普通文件,则为文件名。 如果是目录路径,则为目录名称。
如果路径以 ..
结尾,则返回 None
。
Examples
use std::path::Path;
use std::ffi::OsStr;
assert_eq!(Some(OsStr::new("bin")), Path::new("/usr/bin/").file_name());
assert_eq!(Some(OsStr::new("foo.txt")), Path::new("tmp/foo.txt").file_name());
assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.").file_name());
assert_eq!(Some(OsStr::new("foo.txt")), Path::new("foo.txt/.//").file_name());
assert_eq!(None, Path::new("foo.txt/..").file_name());
assert_eq!(None, Path::new("/").file_name());
Run1.7.0[src]pub fn strip_prefix<P>(&self, base: P) -> Result<&Path, StripPrefixError> where
P: AsRef<Path>,
pub fn strip_prefix<P>(&self, base: P) -> Result<&Path, StripPrefixError> where
P: AsRef<Path>,
返回连接到 base
时产生 self
的路径。
Errors
如果 base
不是 self
的前缀 (即 starts_with
返回 false
),则返回 Err
。
Examples
use std::path::{Path, PathBuf};
let path = Path::new("/test/haha/foo.txt");
assert_eq!(path.strip_prefix("/"), Ok(Path::new("test/haha/foo.txt")));
assert_eq!(path.strip_prefix("/test"), Ok(Path::new("haha/foo.txt")));
assert_eq!(path.strip_prefix("/test/"), Ok(Path::new("haha/foo.txt")));
assert_eq!(path.strip_prefix("/test/haha/foo.txt"), Ok(Path::new("")));
assert_eq!(path.strip_prefix("/test/haha/foo.txt/"), Ok(Path::new("")));
assert!(path.strip_prefix("test").is_err());
assert!(path.strip_prefix("/haha").is_err());
let prefix = PathBuf::from("/test/");
assert_eq!(path.strip_prefix(prefix), Ok(Path::new("haha/foo.txt")));
Run确定 base
是否为 self
的前缀。
仅考虑整个路径组件匹配。
Examples
use std::path::Path;
let path = Path::new("/etc/passwd");
assert!(path.starts_with("/etc"));
assert!(path.starts_with("/etc/"));
assert!(path.starts_with("/etc/passwd"));
assert!(path.starts_with("/etc/passwd/")); // 额外的斜线是可以的
assert!(path.starts_with("/etc/passwd///")); // multiple extra slashes are okay
assert!(!path.starts_with("/e"));
assert!(!path.starts_with("/etc/passwd.txt"));
assert!(!Path::new("/etc/foo.rs").starts_with("/etc/foo"));
Run确定 child
是否为 self
的后缀。
仅考虑整个路径组件匹配。
Examples
use std::path::Path;
let path = Path::new("/etc/resolv.conf");
assert!(path.ends_with("resolv.conf"));
assert!(path.ends_with("etc/resolv.conf"));
assert!(path.ends_with("/etc/resolv.conf"));
assert!(!path.ends_with("/resolv.conf"));
assert!(!path.ends_with("conf")); // 改用 .extension()
Run提取 self.file_name
的茎 (non-extension) 部分。
词干为:
None
,如果没有文件名;- 如果没有嵌入式
.
,则为整个文件名; 否则为 0。 - 如果文件名以
.
开头且内部没有其他.
,则为整个文件名; - 否则,文件名中最后
.
之前的部分
Examples
use std::path::Path;
assert_eq!("foo", Path::new("foo.rs").file_stem().unwrap());
assert_eq!("foo.tar", Path::new("foo.tar.gz").file_stem().unwrap());
Run也可以看看
这个方法类似于 Path::file_prefix
,提取 first .
之前的文件名部分
提取 self.file_name
的前缀。
前缀是:
None
,如果没有文件名;- 如果没有嵌入式
.
,则为整个文件名; 否则为 0。 - 文件名中第一个非开头
.
之前的部分; - 如果文件名以
.
开头且内部没有其他.
,则为整个文件名; - 如果文件名以
.
开头,则为第二个.
之前的文件名部分
Examples
use std::path::Path;
assert_eq!("foo", Path::new("foo.rs").file_prefix().unwrap());
assert_eq!("foo", Path::new("foo.tar.gz").file_prefix().unwrap());
Run也可以看看
此方法类似于 Path::file_stem
,提取文件名中 last .
之前的部分
创建一个拥有的 PathBuf
,并将 path
附加到 self
。
有关连接路径的含义的更多详细信息,请参见 PathBuf::push
。
Examples
use std::path::{Path, PathBuf};
assert_eq!(Path::new("/etc").join("passwd"), PathBuf::from("/etc/passwd"));
Run创建一个拥有的 PathBuf
,例如 self
,但具有给定的文件名。
有关更多详细信息,请参见 PathBuf::set_file_name
。
Examples
use std::path::{Path, PathBuf};
let path = Path::new("/tmp/foo.txt");
assert_eq!(path.with_file_name("bar.txt"), PathBuf::from("/tmp/bar.txt"));
let path = Path::new("/tmp");
assert_eq!(path.with_file_name("var"), PathBuf::from("/var"));
Run创建一个拥有的 PathBuf
,例如 self
,但具有给定的扩展名。
有关更多详细信息,请参见 PathBuf::set_extension
。
Examples
use std::path::{Path, PathBuf};
let path = Path::new("foo.rs");
assert_eq!(path.with_extension("txt"), PathBuf::from("foo.txt"));
let path = Path::new("foo.tar.gz");
assert_eq!(path.with_extension(""), PathBuf::from("foo.tar"));
assert_eq!(path.with_extension("xz"), PathBuf::from("foo.tar.xz"));
assert_eq!(path.with_extension("").with_extension("txt"), PathBuf::from("foo.txt"));
Runpub fn components(&self) -> Components<'_>ⓘNotable traits for Components<'a>impl<'a> Iterator for Components<'a> type Item = Component<'a>;
pub fn components(&self) -> Components<'_>ⓘNotable traits for Components<'a>impl<'a> Iterator for Components<'a> type Item = Component<'a>;
impl<'a> Iterator for Components<'a> type Item = Component<'a>;
生成路径的 Component
上的迭代器。
解析路径时,需要进行少量标准化:
-
重复的分隔符将被忽略,因此
a/b
和a//b
都具有a
和b
作为组件。 -
.
的出现被归一化,除非它们位于路径的开头。 例如,a/./b
,a/b/
,a/b/.
和a/b
都具有a
和b
作为组件,但是./a/b
以附加的CurDir
组件开头。 -
尾部的斜杠已标准化,
/a/b
和/a/b/
是等效的。
请注意,没有其他标准化发生。特别是,a/c
和 a/b/../c
是不同的,以考虑到 b
是符号链接 (因此其父代不是 a
) 的可能性。
Examples
use std::path::{Path, Component};
use std::ffi::OsStr;
let mut components = Path::new("/tmp/foo.txt").components();
assert_eq!(components.next(), Some(Component::RootDir));
assert_eq!(components.next(), Some(Component::Normal(OsStr::new("tmp"))));
assert_eq!(components.next(), Some(Component::Normal(OsStr::new("foo.txt"))));
assert_eq!(components.next(), None)
Run在视为 OsStr
slice 的路径的组件上生成迭代器。
有关如何将路径分成多个组件的详细信息,请参见 components
。
Examples
use std::path::{self, Path};
use std::ffi::OsStr;
let mut it = Path::new("/tmp/foo.txt").iter();
assert_eq!(it.next(), Some(OsStr::new(&path::MAIN_SEPARATOR.to_string())));
assert_eq!(it.next(), Some(OsStr::new("tmp")));
assert_eq!(it.next(), Some(OsStr::new("foo.txt")));
assert_eq!(it.next(), None)
Run查询文件系统以获取有关文件,目录等的信息。
该函数将遍历符号链接以查询有关目标文件的信息。
这是 fs::metadata
的别名。
Examples
use std::path::Path;
let path = Path::new("/Minas/tirith");
let metadata = path.metadata().expect("metadata call failed");
println!("{:?}", metadata.file_type());
Run查询有关文件的元数据,而无需遵循符号链接。
这是 fs::symlink_metadata
的别名。
Examples
use std::path::Path;
let path = Path::new("/Minas/tirith");
let metadata = path.symlink_metadata().expect("symlink_metadata call failed");
println!("{:?}", metadata.file_type());
Run返回路径的规范,绝对形式,所有中间组件均已标准化,符号链接已解析。
这是 fs::canonicalize
的别名。
Examples
use std::path::{Path, PathBuf};
let path = Path::new("/foo/test/../test/bar.rs");
assert_eq!(path.canonicalize().unwrap(), PathBuf::from("/foo/test/bar.rs"));
Run读取符号链接,返回链接指向的文件。
这是 fs::read_link
的别名。
Examples
use std::path::Path;
let path = Path::new("/laputa/sky_castle.rs");
let path_link = path.read_link().expect("read_link call failed");
Run返回目录中条目的迭代器。
迭代器将产生一个 io::Result<fs::DirEntry>
的实例。
最初构造迭代器后,可能会遇到新的错误。
这是 fs::read_dir
的别名。
Examples
use std::path::Path;
let path = Path::new("/laputa");
for entry in path.read_dir().expect("read_dir call failed") {
if let Ok(entry) = entry {
println!("{:?}", entry.path());
}
}
Run如果路径指向现有实体,则返回 true
。
该函数将遍历符号链接以查询有关目标文件的信息。
如果您无法访问文件的元数据,例如
由于权限错误或损坏的符号链接,这将返回 false
。
Examples
use std::path::Path;
assert!(!Path::new("does_not_exist.txt").exists());
Run也可以看看
这是一个方便的函数,可将错误强制为 false。
如果要检查错误,请调用 fs::metadata
。
如果路径指向现有实体,则返回 Ok(true)
。
该函数将遍历符号链接以查询有关目标文件的信息。
如果符号链接断开,则将返回 Ok(false)
。
与 exists()
方法相反,此方法不会默默地忽略与不存在的路径无关的错误。
(E.g.
如果某些父目录的权限被拒绝,它将返回 Err(_)
。)
Examples
#![feature(path_try_exists)]
use std::path::Path;
assert!(!Path::new("does_not_exist.txt").try_exists().expect("Can't check existence of file does_not_exist.txt"));
assert!(Path::new("/root/secret_file.txt").try_exists().is_err());
Run如果路径在磁盘上并且指向常规文件,则返回 true
。
该函数将遍历符号链接以查询有关目标文件的信息。
如果您无法访问文件的元数据,例如由于权限错误或损坏的符号链接,这将返回 false
。
Examples
use std::path::Path;
assert_eq!(Path::new("./is_a_directory/").is_file(), false);
assert_eq!(Path::new("a_file.txt").is_file(), true);
Run也可以看看
这是一个方便的函数,可将错误强制为 false。如果要检查错误,请调用 fs::metadata
并处理其 Result
。
如果是 Ok
,则调用 fs::Metadata::is_file
。
当目标只是读取 (或写入) 源时,可以读取 (或写入) 最可靠的测试源方法是打开它。
例如,仅使用 is_file
才能中断类似 Unix 的系统上的工作流,例如 diff <( prog_a )
。
有关更多信息,请参见 fs::File::open
或 fs::OpenOptions::open
。
如果路径在磁盘上并且指向目录,则返回 true
。
该函数将遍历符号链接以查询有关目标文件的信息。
如果您无法访问文件的元数据,例如
由于权限错误或损坏的符号链接,这将返回 false
。
Examples
use std::path::Path;
assert_eq!(Path::new("./is_a_directory/").is_dir(), true);
assert_eq!(Path::new("a_file.txt").is_dir(), false);
Run也可以看看
这是一个方便的函数,可将错误强制为 false。
如果要检查错误,请调用 fs::metadata
并处理其 Result
。
如果是 Ok
,则调用 fs::Metadata::is_dir
。
如果路径存在于磁盘上并且指向符号链接,则返回 true
。
这个函数不会遍历符号链接。 如果符号链接损坏,这也将返回 true。
如果您无法访问包含该文件的目录,例如,由于权限错误,这将返回 false。
Examples
use std::path::Path;
use std::os::unix::fs::symlink;
let link_path = Path::new("link");
symlink("/origin_does_not_exists/", link_path).unwrap();
assert_eq!(link_path.is_symlink(), true);
assert_eq!(link_path.exists(), false);
Run也可以看看
这是一个方便的函数,可将错误强制为 false。
如果要检查错误,请调用 fs::symlink_metadata
并处理其 Result
。
如果是 Ok
,则调用 fs::Metadata::is_symlink
。
Trait Implementations
fn from(path: &Path) -> Box<Path>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
fn from(path: &Path) -> Box<Path>ⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
从引用创建一个 boxed Path
。
这将为它分配和克隆 path
。
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more
如果存在,则此方法返回 self
和 other
值之间的顺序。 Read more