Why does the compiler output different lifetimes for a characteristic and its implementation?

I am currently using rustlearn to evaluate different classification models. The problem that I encountered is as follows:

trait Foo<T> {
    fn bar(&self, x: T);
}

struct Baz;

struct Smth;

impl<'a> Foo<&'a Smth> for Baz {
    fn bar(&self, x: &Smth) {}
}

fn main() {
    let some_foo: Box<Baz> = Box::new(Baz {});  // ok
    // let some_foo: Box<Foo<&Smth>> = Box::new(Baz {});  // not ok
    let x = Smth {};
    some_foo.bar(&x);
}

While it is possible to enter some_foohow Box<Baz>, it is Box<Foo<&Smth>>impossible to use it instead , as this will cause the compiler to complain about &xwhich will be reset (at the end main) while still occupying (at bar). This (I think) is due to what is being xcreated after some_foo, so it xfalls to some_foo. Moving a creature xin such a way that it occurs before the creature some_foobecomes a solution; but suppose this is not possible.

, some_foo: Box<Baz>, some_foo: Box<Foo<&Smth>>. ( )?


¹, , , .. model.fit(x, y), x, y (X, Y).

+4
1

Smth:

let some_foo: Box<for<'a> Foo<&'a Smth>> = Box::new(Baz {});

, - , :)

+3

Source: https://habr.com/ru/post/1690700/


All Articles