Here is a link to the playground that demonstrates my problem; C<(&str, &str)>
doesn't implement Foo
. The error message is really confusing as well. I have no idea how to solve this.
Well, I have some luck with impl Foo on &C(T) instead of owned C(T). The lifetimes are bound by reference lifetime, instead of higer-ranked "forall". Perhaps more experienced rustaceans can crack this down on owned.
unfortunately I need it to be implemented for the owned C<T>
in my case.
I think this is impossible as written/desired for the same reasons discussed in issue 32330. That is,
- Your
impl
forCollection
is on&'a (A, B)
and notfor<'a> &'a (A, B)
- Necessary since you want to output a reference, analogous to the issue above
- But your trait bound for
C<T>
is onfor<'a> &'a T: Collection
- Because you want to implement for the owned
C<T>
, which has no lifetime to bind
- Because you want to implement for the owned
So the reason C<(&str, &str)>
does not impl Foo
is because the higher-ranked type for<'a> &'a(&str, &str)
does not (and cannot) impl Collection
(even though the bound lifetime type &'a (&str, &str)
does impl Collection
).
I'm hoping someone else here can confirm this. Unfortunately the documentation around higher-ranked trait bounds (HRTB) and higher-rank types also seems to be incomplete, non-existent, or embedded in github issues which largely assume contextual knowledge. But here are a couple more things I found while poking at this in case they are helpful:
- Issue 57362 improved a related error message
-
Issue 56105 discusses some (shifting) interactions involving
&T
andfor<'a> &'a T
Thanks for the response. I'll try to find a workaround.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.