是否可以将包含当前目录 ( .
) 的文件路径字符串与给定目录进行规范化。例如,文件路径 => ./abcd.txt
,给定目录 => /a/b/c
。生成的文件路径的预期结果是/a/b/c/abcd.txt
。
fn main() {
let file_path = "./abcd.txt";
let cur_dir = "/a/b/c";
let path = std::fs::canonicalize(file_path).unwrap();
println!("{:#?}", path);
}
输出
thread 'main' panicked at src/main.rs:5:49:
called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
使用
Path::join
: