Generic slice join

Hi guys,

Is it possible to implement a generic method that could join slices of different types?
In my case I want &[str], &[Path], &[String]

Playground

I could make it generic over an Iterator and write my own join, but I wonder if it's possible to utilize an existing [T]::join ( which is using unstable Join trait)

Thanks.

Path might contain non-UTF-8 data, so it cannot be trivially converted to String. However, it would be possible for the standard library to add a Join implementation that outputs an OsString.

This open issue is closely related:

I'm not sure if this solves your problem entirely, but str, Path, and String all implement AsRef<Path>.

I couldn't make slice::join to work generically, and wrote this instead:

fn join_paths<I>(files: I) -> String
    where I: IntoIterator,
          I::Item: AsRef<Path>
{
    let mut buf = String::new();
    let mut iter = files.into_iter().peekable();
    while let Some(n) = iter.next() {
        buf.push_str(&n.as_ref().to_string_lossy());
        if iter.peek().is_some() {
            buf.push(':');
        }
    }
    buf
}