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) of self.
  • 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.

Trait Implementations

impl<'a, T: ?Sized + AsRef<OsStr>> From<&'a T> for PathBuf

fn from(s: &'a T) -> PathBuf

impl From<OsString> for PathBuf

fn from(s: OsString) -> PathBuf

impl From<String> for PathBuf

fn from(s: String) -> PathBuf

impl<P: AsRef<Path>> FromIterator<P> for PathBuf

fn from_iter<I: IntoIterator<Item=P>>(iter: I) -> PathBuf

impl<P: AsRef<Path>> Extend<P> for PathBuf

fn extend<I: IntoIterator<Item=P>>(&mut self, iter: I)

impl Debug for PathBuf

fn fmt(&self, formatter: &mut Formatter) -> Result<(), Error>

impl Deref for PathBuf

type Target = Path

fn deref(&self) -> &Path

impl Borrow<Path> for PathBuf

fn borrow(&self) -> &Path

impl IntoCow<'static, Path> for PathBuf

fn into_cow(self) -> Cow<'static, Path>

impl PartialEq for PathBuf

fn eq(&self, other: &PathBuf) -> bool

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

impl Eq for PathBuf

impl PartialOrd for PathBuf

fn partial_cmp(&self, other: &PathBuf) -> Option<Ordering>

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

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

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

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

impl Ord for PathBuf

fn cmp(&self, other: &PathBuf) -> Ordering

impl AsRef<OsStr> for PathBuf

fn as_ref(&self) -> &OsStr

impl Into<OsString> for PathBuf

fn into(self) -> OsString

impl AsRef<Path> for PathBuf

fn as_ref(&self) -> &Path

Derived Implementations

impl Hash for PathBuf

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

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

impl Clone for PathBuf

fn clone(&self) -> PathBuf

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