Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

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");
  |          ^^^^
like image 652
Rijenkii Avatar asked Apr 04 '18 12:04

Rijenkii


1 Answers

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.

like image 77
wasmup Avatar answered Nov 22 '22 17:11

wasmup