2

I am seeing a compile error that says:

cannot infer an appropriate lifetime for autoref due to conflicting requirements

I can find plenty of other explanations of this error on the Internet, but one part is still not clear to me: What does "autoref" mean in this context?

dipea
  • 392
  • 4
  • 17

1 Answers1

7

Autoref happens when you attempt to use method syntax to invoke a function when you have a value, but the function takes &self or &mut self -- the method receiver is automatically referenced instead of given by value. For example:

struct Foo;

impl Foo {
    pub fn by_value(self) {}
    pub fn by_ref(&self) {}
    pub fn by_mut(&mut self) {}
}

fn main() {
    let foo = Foo;

    // Autoref to &mut.  These two lines are equivalent.
    foo.by_mut();
    Foo::by_mut(&mut foo);

    // Autoref to &.  These two lines are equivalent.
    foo.by_ref();
    Foo::by_ref(&foo);

    // No autoref since self is received by value.
    foo.by_value();
}

So, in your case you are doing something similar, but the compiler can't come up with a lifetime for the reference that doesn't cause a borrow-check problem.

cdhowie
  • 158,093
  • 24
  • 286
  • 300