1

I want to write a function that takes a vector of any string (borrowed or owned) and returns the unworded 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 collected 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(" ")
}
frederick99
  • 1,033
  • 11
  • 18
  • 1
    join don't exist for Into – Stargateur Dec 27 '20 at 08:38
  • https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=d9c249fa269d2ed57b61a2afa2dacbd0 – Stargateur Dec 27 '20 at 08:39
  • 1
    @Stargateur thanks, it worked. i would never have found out about `Borrow`. you can post it as an answer and i'll accept it. :) – frederick99 Dec 27 '20 at 09:31
  • would someone explain how this question is a duplicate? i dont have a problem with deleting it as Stargateur's solution worked for me. though, i'm curious why sb thought it was a duplicate of an unrelated question. – frederick99 Dec 27 '20 at 14:04
  • Its not a duplicate, someone flagged it wrong. The "answer" there is indeed completely unrelated. – Konstantin W Dec 27 '20 at 20:11
  • @KonstantinW I think I know enough rust to know about what I'm talking about; the question want to consume the vector thus the duplicate totally apply. the deleted answer on this question repeat what the duplicate propose. my comment about borrow is a not a direct answer cause OP ask for something that use into string. The duplicate is more generic and clear, allow a lot of usecase, print an iterator separate by space necessary mean to construct it, join is also used in the duplicate answer; so what about this not being a duplicate ? – Stargateur Dec 27 '20 at 22:37
  • @Stargateur maybe i worded the question poorly, but the point of my question wasn't about consuming the vector or printing a space-separated list. i wanted a function that accepts vectors of both, `&str` and `String` and be able to use trait methods implemented by vector of strings. `Into` is something i found on other post that was about passing either `&str` or `String` directly to a function. – frederick99 Dec 28 '20 at 13:13
  • Into is used to convert something into T. Anyway, If your purpose is just that, why not just call join on the real vector ? you want a custom function that only call join, where you could just call join directly ? you make your life complicated. If your question is rephrase in why this doesn't compile with some rephrasing in the body, yes I would say it's not a duplicate, but currently the answer in the duplicate solve your question in a better way. Currently your question is "I want to write a function that takes a vector of any string (borrowed or owned) and returns the unworded string." – Stargateur Dec 28 '20 at 18:48
  • 1
    because using duplicate here the actual answer to your question, https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=24e1964849aaf2fcdfa1fbebe15c60db – Stargateur Dec 28 '20 at 18:56
  • @Stargateur cool, if you say so (even though none of the solutions that worked are in the referenced post.) i could call join directly if it was the only thing that i was doing, but i've elided unnecessary details intentionally. i dont think i'm complicating it or my life by extracting repeated code in a function. – frederick99 Dec 29 '20 at 08:20

0 Answers0