An owned (heap-allocated) path buffer
Example
use std::fs::{PathBuf, Path};
let path = PathBuf::from_str("/home");
defer path.free();
path.push(Path::new("alumina/"));
assert_eq!(path.as_path(), Path::new("/home/alumina"));
path.push(Path::new("/hello/world"));
assert_eq!(path.as_path(), Path::new("/hello/world"));
Run this example
Fields
-
inner: StringBuf
Methods
impl PathBuf { ... }
-
fn new() -> PathBuf
Create a new empty path buffer
-
Create a new path buffer from a Path instance
Example
use std::fs::{PathBuf, Path}; let path = PathBuf::from_path(Path::new("/home/alumina")); defer path.free(); assert_eq!(path.as_path(), Path::new("/home/alumina"));
Run this example -
Create a new path buffer from a string slice
Example
use std::fs::{PathBuf, Path}; let path = PathBuf::from_str("/home/alumina"); defer path.free(); assert_eq!(path.as_path(), Path::new("/home/alumina"));
Run this example -
View path as Path object
Example
use std::fs::{PathBuf, Path}; let path = PathBuf::from_str("/home/alumina"); defer path.free(); assert_eq!(path.as_path(), Path::new("/home/alumina"));
Run this example -
fn extend<It>(self: &mut PathBuf, iter: &mut It)
It: Iterator<It, PathSegment>Extend the path buffer from an iterator of path segments
Example
use std::fs::{PathBuf, Path, PathSegment}; let path = PathBuf::from_str("/home"); defer path.free(); path.extend(&[ PathSegment::parent_dir(), PathSegment::current_dir(), PathSegment::normal("alumina"), ].iter()); assert_eq!(path.as_path(), Path::new("/home/.././alumina"));
Run this example -
Create a new path buffer with the base path of
self
and extensionsuffix
.Example
use std::fs::{PathBuf, Path}; let path = PathBuf::from_str("/home/alumina"); defer path.free(); let new_path = path.with_suffix(Path::new("foo/bar")); defer new_path.free(); assert_eq!(new_path.as_path(), Path::new("/home/alumina/foo/bar"));
Run this example -
Extend the path buffer with a Path instance
Example
use std::fs::{PathBuf, Path}; let path = PathBuf::from_str("/home"); defer path.free(); path.push(Path::new("alumina")); assert_eq!(path.as_path(), Path::new("/home/alumina"));
Run this example -
Pop the last path segment from the path buffer
Returns
true
if the path buffer was modified,false
otherwise.Example
use std::fs::{PathBuf, Path}; let path = PathBuf::from_str("/home/alumina"); defer path.free(); assert!(path.pop()); assert_eq!(path.as_path(), Path::new("/home")); assert!(path.pop()); assert_eq!(path.as_path(), Path::new("/")); assert!(!path.pop()); assert_eq!(path.as_path(), Path::new("/"));
Run this example -
fn from_iter<It>(iter: &mut It) -> PathBuf
It: Iterator<It, PathSegment>Create a new path buffer from an iterator of path segments
Example
use std::fs::{PathBuf, Path, PathSegment}; let path = PathBuf::from_iter(&[ PathSegment::normal("home"), PathSegment::normal("alumina"), ].iter()); defer path.free(); assert_eq!(path.as_path(), Path::new("/home/alumina"));
Run this example -
Returns a copy of the object.
-
Returns a view to the object.
-
fn free(self: &mut PathBuf)
Frees the memory backing the object.