A method named `join` was not 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 this, 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");
  |          ^^^^
+4
source share
1 answer

Try this :

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

cm

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

Conclusion:

"./foo/bar/13123123"

See the documentation forAsRef .

+4
source

Source: https://habr.com/ru/post/1695756/


All Articles