type TarPathBuf = PathBuf;
Aliased Type§
struct TarPathBuf { /* private fields */ }
Implementations
source§impl PathBuf
impl PathBuf
1.44.0 · sourcepub fn with_capacity(capacity: usize) -> PathBuf
pub fn with_capacity(capacity: usize) -> PathBuf
Creates a new PathBuf
with a given capacity used to create the
internal OsString
. See with_capacity
defined on OsString
.
§Examples
use std::path::PathBuf;
let mut path = PathBuf::with_capacity(10);
let capacity = path.capacity();
// This push is done without reallocating
path.push(r"C:\");
assert_eq!(capacity, path.capacity());
sourcepub fn leak<'a>(self) -> &'a mut Path
🔬This is a nightly-only experimental API. (os_string_pathbuf_leak
)
pub fn leak<'a>(self) -> &'a mut Path
os_string_pathbuf_leak
)Consumes and leaks the PathBuf
, returning a mutable reference to the contents,
&'a mut Path
.
The caller has free choice over the returned lifetime, including ’static. Indeed, this function is ideally used for data that lives for the remainder of the program’s life, as dropping the returned reference will cause a memory leak.
It does not reallocate or shrink the PathBuf
, so the leaked allocation may include
unused capacity that is not part of the returned slice. If you want to discard excess
capacity, call into_boxed_path
, and then Box::leak
instead.
However, keep in mind that trimming the capacity may result in a reallocation and copy.
1.0.0 · sourcepub fn push<P>(&mut self, path: P)
pub fn push<P>(&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 replacesself
. - if
self
has a verbatim prefix (e.g.\\?\C:\windows
) andpath
is not empty, the new path is normalized: all references to.
and..
are removed.
Consider using Path::join
if you need a new PathBuf
instead of
using this function on a cloned PathBuf
.
§Examples
Pushing a relative path extends the existing path:
use std::path::PathBuf;
let mut path = PathBuf::from("/tmp");
path.push("file.bk");
assert_eq!(path, PathBuf::from("/tmp/file.bk"));
Pushing an absolute path replaces the existing path:
use std::path::PathBuf;
let mut path = PathBuf::from("/tmp");
path.push("/etc");
assert_eq!(path, PathBuf::from("/etc"));
1.0.0 · sourcepub fn pop(&mut self) -> bool
pub fn pop(&mut self) -> bool
Truncates self
to self.parent
.
Returns false
and does nothing if self.parent
is None
.
Otherwise, returns 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);
1.0.0 · sourcepub fn set_file_name<S>(&mut self, file_name: S)
pub fn set_file_name<S>(&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
.
Otherwise it is equivalent to calling pop
and then pushing
file_name
. The new path will be a sibling of the original path.
(That is, it will have the same parent.)
The argument is not sanitized, so can include separators. This behaviour may be changed to a panic in the future.
§Examples
use std::path::PathBuf;
let mut buf = PathBuf::from("/");
assert!(buf.file_name() == None);
buf.set_file_name("foo.txt");
assert!(buf == PathBuf::from("/foo.txt"));
assert!(buf.file_name().is_some());
buf.set_file_name("bar.txt");
assert!(buf == PathBuf::from("/bar.txt"));
buf.set_file_name("baz");
assert!(buf == PathBuf::from("/baz"));
buf.set_file_name("../b/c.txt");
assert!(buf == PathBuf::from("/../b/c.txt"));
buf.set_file_name("baz");
assert!(buf == PathBuf::from("/../b/baz"));
1.0.0 · sourcepub fn set_extension<S>(&mut self, extension: S) -> bool
pub fn set_extension<S>(&mut self, extension: S) -> bool
Updates self.extension
to Some(extension)
or to None
if
extension
is empty.
Returns false
and does nothing if self.file_name
is None
,
returns true
and updates the extension otherwise.
If self.extension
is None
, the extension is added; otherwise
it is replaced.
If extension
is the empty string, self.extension
will be None
afterwards, not Some("")
.
§Panics
Panics if the passed extension contains a path separator (see
is_separator
).
§Caveats
The new extension
may contain dots and will be used in its entirety,
but only the part after the final dot will be reflected in
self.extension
.
If the file stem contains internal dots and extension
is empty, part
of the old file stem will be considered the new self.extension
.
See the examples below.
§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());
p.set_extension("cookie");
assert_eq!(Path::new("/feel/the.dark.cookie"), p.as_path());
p.set_extension("");
assert_eq!(Path::new("/feel/the.dark"), p.as_path());
p.set_extension("");
assert_eq!(Path::new("/feel/the"), p.as_path());
p.set_extension("");
assert_eq!(Path::new("/feel/the"), p.as_path());
sourcepub fn add_extension<S>(&mut self, extension: S) -> bool
🔬This is a nightly-only experimental API. (path_add_extension
)
pub fn add_extension<S>(&mut self, extension: S) -> bool
path_add_extension
)Append self.extension
with extension
.
Returns false
and does nothing if self.file_name
is None
,
returns true
and updates the extension otherwise.
§Caveats
The appended extension
may contain dots and will be used in its entirety,
but only the part after the final dot will be reflected in
self.extension
.
See the examples below.
§Examples
#![feature(path_add_extension)]
use std::path::{Path, PathBuf};
let mut p = PathBuf::from("/feel/the");
p.add_extension("formatted");
assert_eq!(Path::new("/feel/the.formatted"), p.as_path());
p.add_extension("dark.side");
assert_eq!(Path::new("/feel/the.formatted.dark.side"), p.as_path());
p.set_extension("cookie");
assert_eq!(Path::new("/feel/the.formatted.dark.cookie"), p.as_path());
p.set_extension("");
assert_eq!(Path::new("/feel/the.formatted.dark"), p.as_path());
p.add_extension("");
assert_eq!(Path::new("/feel/the.formatted.dark"), p.as_path());
1.70.0 · sourcepub fn as_mut_os_string(&mut self) -> &mut OsString
pub fn as_mut_os_string(&mut self) -> &mut OsString
Yields a mutable reference to the underlying OsString
instance.
§Examples
use std::path::{Path, PathBuf};
let mut path = PathBuf::from("/foo");
path.push("bar");
assert_eq!(path, Path::new("/foo/bar"));
// OsString's `push` does not add a separator.
path.as_mut_os_string().push("baz");
assert_eq!(path, Path::new("/foo/barbaz"));
1.0.0 · sourcepub fn into_os_string(self) -> OsString
pub fn into_os_string(self) -> OsString
1.63.0 · sourcepub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError>
Invokes try_reserve
on the underlying instance of OsString
.
1.44.0 · sourcepub fn reserve_exact(&mut self, additional: usize)
pub fn reserve_exact(&mut self, additional: usize)
Invokes reserve_exact
on the underlying instance of OsString
.
1.63.0 · sourcepub fn try_reserve_exact(
&mut self,
additional: usize,
) -> Result<(), TryReserveError>
pub fn try_reserve_exact( &mut self, additional: usize, ) -> Result<(), TryReserveError>
Invokes try_reserve_exact
on the underlying instance of OsString
.
1.44.0 · sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Invokes shrink_to_fit
on the underlying instance of OsString
.
Trait Implementations
source§impl Arg for PathBuf
impl Arg for PathBuf
source§fn to_string_lossy(&self) -> Cow<'_, str>
fn to_string_lossy(&self) -> Cow<'_, str>
Cow<'_, str>
.source§fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>, Errno>
fn as_cow_c_str(&self) -> Result<Cow<'_, CStr>, Errno>
CStr
.source§impl<'de> Deserialize<'de> for PathBuf
impl<'de> Deserialize<'de> for PathBuf
source§fn deserialize<D>(
deserializer: D,
) -> Result<PathBuf, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D,
) -> Result<PathBuf, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
1.0.0 · source§impl<P> Extend<P> for PathBuf
impl<P> Extend<P> for PathBuf
source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = P>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = P>,
source§fn extend_one(&mut self, p: P)
fn extend_one(&mut self, p: P)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)