我有一个函数需要 AsRef<Path>
作为参数,看起来像这样
fn test<P: AsRef<std::path::Path>>(path: P) {
path.join("13123123");
}
当我编译它时,它给了我以下错误
error[E0599]: no method named `join` found for type `P` in the current scope
--> src/main.rs:2:10
|
2 | path.join("13123123");
| ^^^^
最佳答案
尝试 this :
path.as_ref().join("13123123")
见:
fn main() {
let path = std::path::Path::new("./foo/bar/");
test(path);
}
fn test<P: AsRef<std::path::Path>>(path: P) {
println!("{:?}", path.as_ref().join("13123123"));
}
输出:
"./foo/bar/13123123"
关于rust - 找不到 AsRef<Path> 的名为 `join` 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49650660/