Rijenkii
Rijenkii

Reputation: 178

No method named `join` found for AsRef<Path>

I have a function that takes AsRef<Path> as an argument and looks like this

fn test<P: AsRef<std::path::Path>>(path: P) {
    path.join("13123123");
}

When I compile that, it gives me the following error

error[E0599]: no method named `join` found for type `P` in the current scope
 --> src/main.rs:2:10
  |
2 |     path.join("13123123");
  |          ^^^^

Upvotes: 5

Views: 1219

Answers (1)

wasmup
wasmup

Reputation: 16253

Try this:

path.as_ref().join("13123123")

see:

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"));
}

Output:

"./foo/bar/13123123"

See the documentation for AsRef.

Upvotes: 5

Related Questions