我有一个以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");
| ^^^^
答案 0 :(得分:4)
尝试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"