Struct std::path::PathBufStable
[-] [+]
[src]
pub struct PathBuf { // some fields omitted }
An owned, mutable path (akin to String
).
This type provides methods like push
and set_extension
that mutate the
path in place. It also implements Deref
to Path
, meaning that all
methods on Path
slices are available on PathBuf
values as well.
More details about the overall approach can be found in the module documentation.
Examples
extern crate std; fn main() { use std::path::PathBuf; let mut path = PathBuf::from("c:\\"); path.push("windows"); path.push("system32"); path.set_extension("dll"); }use std::path::PathBuf; let mut path = PathBuf::from("c:\\"); path.push("windows"); path.push("system32"); path.set_extension("dll");
Methods
impl PathBuf
fn new() -> PathBuf
Allocates an empty PathBuf
.
fn as_path(&self) -> &Path
Coerces to a Path
slice.
fn push<P: AsRef<Path>>(&mut self, path: P)
Extends self
with path
.
If path
is absolute, it replaces the current path.
On Windows:
- if
path
has a root but no prefix (e.g.\windows
), it replaces everything except for the prefix (if any) ofself
. - if
path
has a prefix but no root, it replaces `self.
fn pop(&mut self) -> bool
Truncate self
to self.parent()
.
Returns false and does nothing if self.file_name()
is None
.
Otherwise, returns true
.
fn set_file_name<S: AsRef<OsStr>>(&mut self, file_name: S)
Updates self.file_name()
to file_name
.
If self.file_name()
was None
, this is equivalent to pushing
file_name
.
Examples
extern crate std; fn main() { 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")); }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"));
fn set_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> bool
Updates self.extension()
to extension
.
If self.file_name()
is None
, does nothing and returns false
.
Otherwise, returns true
; if self.extension()
is None
, the extension
is added; otherwise it is replaced.
fn into_os_string(self) -> OsString
Consumes the PathBuf
, yielding its internal OsString
storage.