Я гоняюсь ошибка компилятора и нашел следующий пример
trait Lt<'a> {
type T;
}
impl<'a> Lt<'a> for () {
type T = &'a ();
}
fn test() {
let _: fn(<() as Lt<'_>>::T) = |_: &'static ()| {};
}
fn main() {
test();
}
Я ожидаю, что вышеприведенное скомпилируется, поскольку я дал подсказку, что Lt<'_>
будет Lt<'static>
, поэтому все должно быть в порядке, но я получаю следующую ошибку:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
--> src/main.rs:10:53
|
10 | let _: fn(<() as Lt<'_>>::T) = |_: &'static ()| {};
| ^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the body at 10:36...
--> src/main.rs:10:36
|
10 | let _: fn(<() as Lt<'_>>::T) = |_: &'static ()| {};
| ^^^^^^^^^^^^^^^^^^^
= note: ...so that the types are compatible:
expected Lt<'_>
found Lt<'_>
= note: but, the lifetime must be valid for the static lifetime...
= note: ...so that the types are compatible:
expected &()
found &'static ()
Какова логика «во-первых, время жизни не может пережить анонимное время жизни №2»? Поскольку я смотрю на вариант ошибки, если причина не является твердой, мы можем попытаться ее исправить.
Рабочая вариация
fn test() {
let _: fn(<() as Lt<'static>>::T) = |_: &'_ ()| {};
}