I am getting a “cannot infer an appropriate lifetime due to conflicting requirements” error. Here is an example. It may not be minimal but it mirrors the structure of my actual code pretty closely.
I only sort of see the conflict and unfortunately this particular message doesn’t provide more specific information. I believe it has something to do with using
Ex::mutate since if I remove all the slices and change the
Vec to just a reference, everything compiles fine. I think it has to do with how
Borrow works with
&mut self too, since if I simply use
get instead of
get_mut, everything compiles fine.
I looked at mutability affects lifetime inference which sounds very similar but has no actionable resolution.
Given the error message, my working theory is that the compiler is trying to assign the lifetime of the references in the
Vec to the references in the
s parameter of
Ex::mutate. But I don’t see why it should assign those, nor why the lifetimes must be the same (if indeed they should). My intuition says they should be allowed to be unrelated since I only need to hash the input and nothing else.
If I make the compiler-suggested change and annotate
fn mutate(&mut self, &[&'a str]), the error moves up a level to
Container::mutate where it again wants me to annotate the parameter. Making that changes pushes the error another level to
Library::func, which in my actual code is third-party and cannot be changed without failing to implement the library’s trait. It is the library which actually owns the “root”
&str from which all further slices are taken.
I can’t change the library code, and I’d like my
Ex::mutate signature to look basically the same (slices with references in parameters). I have tried adding various explicit lifetimes and bounds but have yet to find a version that works.
So my questions are:
- What exactly is going on with the lifetimes?
- Is there any way to express to the compiler that there is no conflict?
- If not, is there an alternate construction that can support the same basic API? I haven’t been able to find one.