To find the path of a file in Rust, you need to use the std::path::PathBuf
and std::fs::canonicalize()
functions. The PathBuf
is a struct that represents a path on the file system, and canonicalize()
returns the canonicalized absolute path of the given PathBuf
.
Here's an example code that finds the path of a file named example.txt
in the current directory:
main.rs322 chars12 lines
In this code, we first create a PathBuf
from the filename example.txt
. Then, we try to find the full path of the file using fs::canonicalize()
function. If canonicalize()
returns Ok
, we print the full path. Otherwise, we print the error.
gistlibby LogSnag