Struct std::path::PathBuf 1.0.0[−][src]
pub struct PathBuf { /* fields omitted */ }Expand description
拥有的可变路径 (类似于 String)。
这种类型提供了 push 和 set_extension 之类的方法,这些方法会改变路径。
它还实现了 Deref 到 Path,这意味着 Path 切片上的所有方法也可以在 PathBuf 值上使用。
有关整体方法的更多详细信息,请参见 模块级文档。
Examples
您可以使用 push 从组件构建 PathBuf:
use std::path::PathBuf;
let mut path = PathBuf::new();
path.push(r"C:\");
path.push("windows");
path.push("system32");
path.set_extension("dll");Run但是,push 最适合用于动态情况。
当您提前了解所有组件时,这是一种更好的方法:
use std::path::PathBuf;
let path: PathBuf = [r"C:\", "windows", "system32.dll"].iter().collect();Run我们仍然可以做得更好! 由于这些都是字符串,因此我们可以使用
From::from:
use std::path::PathBuf;
let path = PathBuf::from(r"C:\windows\system32.dll");Run哪种方法最有效取决于您所处的情况。
Implementations
创建具有给定容量的新 PathBuf,用于创建内部 OsString。
请参见在 OsString 上定义的 with_capacity。
Examples
use std::path::PathBuf;
let mut path = PathBuf::with_capacity(10);
let capacity = path.capacity();
// 无需重新分配即可完成此推送
path.push(r"C:\");
assert_eq!(capacity, path.capacity());Run用 path 扩展 self。
如果 path 是绝对路径,它将替换当前路径。
在 Windows 上:
-
如果
path具有根但没有前缀 (例如\windows),它将替换self的前缀 (如果有) 之外的所有内容。 -
如果
path有前缀但没有根,它将替换self。 -
如果
self有 verbatim 前缀 (例如\\?\C:\windows) 并且path不为空,新路径被规范化:所有对.和..的引用都被删除。
Examples
推动相对路径可扩展现有路径:
use std::path::PathBuf;
let mut path = PathBuf::from("/tmp");
path.push("file.bk");
assert_eq!(path, PathBuf::from("/tmp/file.bk"));Run推送绝对路径将替换现有路径:
use std::path::PathBuf;
let mut path = PathBuf::from("/tmp");
path.push("/etc");
assert_eq!(path, PathBuf::from("/etc"));Run将 self 截断为 self.parent。
返回 false,如果 self.parent 为 None,则不执行任何操作。
否则,返回 true。
Examples
use std::path::{Path, PathBuf};
let mut p = PathBuf::from("/spirited/away.rs");
p.pop();
assert_eq!(Path::new("/spirited"), p);
p.pop();
assert_eq!(Path::new("/"), p);Run将 self.file_name 更新为 file_name。
如果 self.file_name 是 None,则等效于按下 file_name。
否则,这等效于调用 pop,然后按 file_name。
新路径将是原始路径的同级。
(也就是说,它将具有相同的父对象。)
Examples
use std::path::PathBuf;
let mut buf = PathBuf::from("/");
assert!(buf.file_name() == None);
buf.set_file_name("bar");
assert!(buf == PathBuf::from("/bar"));
assert!(buf.file_name().is_some());
buf.set_file_name("baz.txt");
assert!(buf == PathBuf::from("/baz.txt"));Run将 self.extension 更新为 extension。
返回 false,如果 self.file_name 为 None,则不执行任何操作,否则返回 true,并更新扩展名。
如果 self.extension 为 None,则添加扩展名; 否则将被替换。
Examples
use std::path::{Path, PathBuf};
let mut p = PathBuf::from("/feel/the");
p.set_extension("force");
assert_eq!(Path::new("/feel/the.force"), p.as_path());
p.set_extension("dark_side");
assert_eq!(Path::new("/feel/the.dark_side"), p.as_path());Run1.20.0[src]pub fn into_boxed_path(self) -> 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>
pub fn into_boxed_path(self) -> 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>在 OsString 的底层实例上调用 reserve_exact。
在 OsString 的底层实例上调用 shrink_to_fit。
Methods from Deref<Target = Path>
将 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(p: PathBuf) -> 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(p: PathBuf) -> 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>将 PathBuf 转换为 Box<Path>
此转换当前不应该分配内存,但是不能在所有平台上或所有 future 版本中都保证此行为。
从迭代器创建一个值。 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