rust - 找不到 AsRef<Path> 的名为 `join` 的方法

标签 rust

我有一个函数需要 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"

参见 documentation for AsRef .

关于rust - 找不到 AsRef<Path> 的名为 `join` 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49650660/

相关文章:

rust - web_sys crate 是如何工作的?

rust - 映射潜在故障的好方法

rust - 是否可以选择让 rustc 显示 "successful"消息?

parsing - 使用 `nom` 处理自定义枚举类型是否有意义?

rust - 仅按变体比较枚举,不按值比较

windows - 为什么我不能在 Rust 中将 `Stdio::piped()` 与 windows `cmd.exe` 一起使用?

compiler-errors - 从方法内部的模式匹配返回时在当前范围内找不到关联类型

rust - Rustfmt 是否可以选择使类型显式化?

initialization - 是否可以声明一个元组结构,其成员是私有(private)的,除了初始化?

rust - 传递可变值的惯用方法是什么?