Documentation provides following example for joining paths:
use std::path::PathBuf;
let path: PathBuf = [r"C:\", "windows", "system32.dll"].iter().collect();
This works when all the components are strings. However, I am trying to write following function:
use std::path::PathBuf;
fn my_path<P: AsRef<Path>>(root: P, dir1: &str, dir2: &str, dir3: &str) -> PathBuf {
[root, dir1, dir2, dir3].iter().collect()
}
The above obviously doesn't work. I know I can do series of nested joins, but that is, ..., more ugly.
Is there a way to join different path-like components in array?
You can cast them to dynamic AsRef<Path> objects:
use std::path::{Path, PathBuf};
fn my_path<P: AsRef<Path>>(root: P, dir1: &str, dir2: &str, dir3: &str) -> PathBuf {
[&root as &dyn AsRef<Path>, &dir1, &dir2, &dir3].iter().collect()
}
or just add the first different object with join:
use std::path::{Path, PathBuf};
fn my_path<P: AsRef<Path>>(root: P, dir1: &str, dir2: &str, dir3: &str) -> PathBuf {
root.as_ref().join([dir1, dir2, dir3].iter().collect::<PathBuf>())
}
Here it is on Rust Playground
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With