I want to write a function that takes a vector of any string (borrowed or owned) and returns the unword
ed string.
fn unwords<S: Into<String>>(vec: Vec<S>) -> String
{
vec.join(" ")
}
But, I am getting a trait unsatisfied error:
error[E0599]: no method named `join` found for struct
`Vec<S>` in the current scope
--> src\lib.rs:85:18
|
85 | Some(vec.join(" "))
| ^^^^ method not found in `Vec<S>`
|
= note: the method `join` exists but the following
trait bounds were not satisfied:
`<[S] as Join<_>>::Output = _`
I have tried various constraints in the where clause but to no avail.
Update:
The answer in the comments does not work with the latest version of rust anymore. The Map
has to be collect
ed in a Vec
first.
With some type juggling, I've managed to make it work this time (with nightly rust). Personally, not a big fan of all the noise but it is what it is.
#![feature(slice_concat_trait)]
use std::slice::Join;
pub fn unwords<'a, S>(vec: Vec<S>) -> <[S] as Join<&'a str>>::Output where [S]: Join<&'a str>
{
vec.join(" ")
}