I was trying to write a generic function that can join a slice of either owned strings or string slices. I didn't know what the appropriate trait bound should be, so I started with AsRef<str>
:
fn join<S>(slice: &[S]) -> String
where
S: AsRef<str>,
{
slice.join(" ")
}
But that yielded this compile error:
error[E0599]: no method named `join` found for reference `&[S]` in the current scope
--> src/main.rs:5:11
|
5 | slice.join(" ")
| ^^^^ method not found in `&[S]`
|
= note: the method `join` exists but the following trait bounds were not satisfied:
`<[S] as std::slice::Join<_>>::Output = _`
I'm not sure how to interpet the hint on which trait bounds were not satisfied, i.e. <[S] as std::slice::Join<_>>::Output = _
. Eventually, I decided to look up impls of std::slice::Join
in the docs, and there I noticed that the one I need has a trait bound on S: Borrow<str>
, so I changed my own trait bound to that and that did the trick.
However, I still don't understand how I should have inferred that from the <[S] as std::slice::Join<_>>::Output = _
hint. Any advice on how I should read this type of hint in the future would be greatly appreciated What is the compiler trying to tell me by including this information?