I have a Vec
of my custom type, which I will call Name
. The type can be represented compactly as a String
, and I would like to be able to call .join()
on a Vec
of this type. This actually works when I implement Borrow<str>
for my type.
But during tests, I also have Vec
s containing references to my type so that I can concisely re-use variables. While .join(", ")
works with Vec<Name>
, trying to use Vec<&Name>
instead gives me an error:
Compiling playground v0.0.1 (/playground)
error[E0599]: the method `join` exists for struct `Vec<&Name>`, but its trait bounds were not satisfied
--> src/main.rs:43:39
|
45 | println!("Expected: {}", expected.join(", "));
| ^^^^ method cannot be called on `Vec<&Name>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`[&Name]: Join<_>`
For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` (bin "playground") due to 1 previous error
Here's a Rust Playground.
If there's a better way to handle re-using the instances in the tests so that I can have Vecs of owned types, then I'm all ears. But otherwise, is there a succinct way to get join()
working on a Vec
of references? ChatGPT wants me to map the items to a new Vec<String>
using to_string()
on each and then join()
that, which does work… but feels much clunkier than the concise .join()
I'm hoping for.
Thanks in advance!