In Rust, to convert a PathBuf
into a Path
, simply use the .as_path()
method.
main.rs159 chars8 lines
In the code above, we create a PathBuf
variable path_buf
and assign it the value of example.txt
.
We then get a reference to the Path
representation of the PathBuf
object by calling .as_path()
on it, and store it in the variable path
.
The code then prints out the Path
object using println!
and the {:?}
format specifier.
This will output:
main.rs14 chars2 lines
Note: The as_path()
method only returns a reference to the Path
object, and does not allocate memory for a new copy of the path.
gistlibby LogSnag