Rust PathBuf Examples
These are examples of the functions and features available in Rusts PathBuf
Note
This is a work in progress making examples from
Pop
Code
use PathBuf;
Results
/Users/alan/Desktop
NOTE: the pop just returns true not the item that was popped as far as I can tell
Set Extension
Code
fn main() {
let mut path = PathBuf::from("/Users/alan/Desktop/alfa.v1.txt");
path.set_extension("neo");
dbg! {path};
}
Results
/Users/alan/Desktop/alfa.v1.neo
Set File Name
Code
Results
/Users/alan/Desktop/bravo.v2.txt
Ancestors
Code
Results
/Users/alan/Desktop/alfa.v1.txt
/Users/alan/Desktop
/Users/alan
/Users
/
Canonicalize
Code
Results
/Users/alan/Desktop/alfa.v1.txt
Components
Code
fn main() {
let path = PathBuf::from("/Users/alan/Desktop/alfa.v1.txt");
let components = path.components();
dbg! {components};
}
Results
// Makes an iterator that produces this
Components(
[
RootDir,
Normal(
"Users",
),
Normal(
"alan",
),
Normal(
"Desktop",
),
Normal(
"alfa.v1.txt",
),
],
)
Display Name
Code
Results
/Users/alan/Desktop/alfa.v1.txt
Extension
Code
Results
txt
File Name
Code
Results
alfa.v1.txt
File Stem
(There's a `file_prefix`` that's in nightly as well that will do a similar thing as thise but return `alfa`` instead)
Code
Results
alfa.v1
Join Paths
This creates a new path from the joined parts
Code
Results
/Users/alan/Desktop/alfa.v1.txt
Metadata
Code
Results
Metadata {
file_type: FileType(
FileType {
mode: 33188,
},
),
is_dir: false,
is_file: true,
permissions: Permissions(
FilePermissions {
mode: 33188,
},
),
accessed: Ok(
SystemTime {
tv_sec: 1680741445,
tv_nsec: 46423762,
},
),
..
}
Parent
Code
Results
/Users/alan/Desktop/alfa.v1.txt
Strip Prefix
Code
Results
Desktop/alfa.v1.txt
A basic rundown of Rust path features