Rust 为什么这一辈子不能活过结束?

Rust 为什么这一辈子不能活过结束?,rust,lifetime,Rust,Lifetime,我在追,发现了以下几点 trait Lt所以一切都应该正常,但我得到以下错误: error[E0495]:由于需求冲突,无法为生存期参数“%a”推断适当的生存期 -->src/main.rs:10:53 | 10 | let |:fn(src/main.rs:10:36 | 10 |让|:fn( =注意:但是,生存期必须对静态生存期有效。。。 =注意:…以便类型兼容: 预期&() 找到&'static() “首先,生命周期不能超过匿名生命周期#2”背后的逻辑是什么?当我看到一个bug的变体时,

我在追,发现了以下几点

trait Lt
所以一切都应该正常,但我得到以下错误:

error[E0495]:由于需求冲突,无法为生存期参数“%a”推断适当的生存期
-->src/main.rs:10:53
|
10 | let |:fn(src/main.rs:10:36
|
10 |让|:fn(
=注意:但是,生存期必须对静态生存期有效。。。
=注意:…以便类型兼容:
预期&()
找到&'static()
“首先,生命周期不能超过匿名生命周期#2”背后的逻辑是什么?当我看到一个bug的变体时,如果原因不可靠,我们可以尝试修复它

工作变化
fn测试(){

下面的代码片段是您案例的一个组合,它帮助我理解 在代码中使用
静态
生存期声明时出现编译错误的问题

struct Foo {
}

fn _test_ok() {

    // myf is declared as a function pointer that accepts
    // a reference with some generic lifetime  
    let myf: fn(&'_ Foo);

    // with this definition ...
    myf = |_arg: &'_ Foo| {};

    // this local value works as expected
    let local_foo: Foo = Foo {};

    myf(&local_foo);
}

fn _test_static_fail() {

    let myf: fn(&'_ Foo);

    // suppose this myf() definition ...
    myf = |_arg: &'static Foo| {};

    // this local value is compatible with myf() declaration ...
    let local_foo: Foo = Foo {};

    // so theoretically it is usable here: 
    myf(&local_foo);

    // but this is clearly not possible because the anomymous lifetime region where 
    // local_foo lives does not outlive the 'static lifetime required
    // when defining myf()   
}


static FOO: Foo = Foo {};

fn _test_static_flipped() {

    // As you have already discovered
    let myf: fn(&'static Foo) = |arg: &'_ Foo| {};

    // this works because ...
    // 1. FOO is a static value and it is compatible with myf() definition and
    // 2. 'static lifetime outlives every other lifetime  
    myf(&FOO);
}

fn main() {}

这取决于
fn(&”ufoo)
总是被分解为
,因为一般来说,我希望在
fn(&”ufoo)中
匿名生存期
'
是下界的,就像本例中的
for那样,如果是这样,那么
'static
似乎不包括在
定义的生存期集合中。目前,Rust不支持这些东西。我们可以将
for
的static
包含在
for中
fn test() {
    let _: fn(<() as Lt<'static>>::T) = |_: &'_ ()| {};
}
struct Foo {
}

fn _test_ok() {

    // myf is declared as a function pointer that accepts
    // a reference with some generic lifetime  
    let myf: fn(&'_ Foo);

    // with this definition ...
    myf = |_arg: &'_ Foo| {};

    // this local value works as expected
    let local_foo: Foo = Foo {};

    myf(&local_foo);
}

fn _test_static_fail() {

    let myf: fn(&'_ Foo);

    // suppose this myf() definition ...
    myf = |_arg: &'static Foo| {};

    // this local value is compatible with myf() declaration ...
    let local_foo: Foo = Foo {};

    // so theoretically it is usable here: 
    myf(&local_foo);

    // but this is clearly not possible because the anomymous lifetime region where 
    // local_foo lives does not outlive the 'static lifetime required
    // when defining myf()   
}


static FOO: Foo = Foo {};

fn _test_static_flipped() {

    // As you have already discovered
    let myf: fn(&'static Foo) = |arg: &'_ Foo| {};

    // this works because ...
    // 1. FOO is a static value and it is compatible with myf() definition and
    // 2. 'static lifetime outlives every other lifetime  
    myf(&FOO);
}

fn main() {}