简体   繁体   English

没有附加变量的生命周期是什么意思?

[英]what does lifetime without a variable attached mean?

fn hello<'a>() {}

In rust, can a function have its own lifetime?在 rust 中,function 可以有自己的生命周期吗? I ran into a piece of code as below我遇到了一段代码如下

struct Hi {}

async fn wrapper<'a, F, Fut>(f: F) 
where 
Fut: Future<Output = ()>,
F: FnOnce(&'a mut Hi) -> Fut{

}

but couldn't understand what lifetime 'a could mean here...但无法理解 life 'a 在这里可能意味着什么......

can a function have its own lifetime? function 可以有自己的寿命吗?

Functions don't have lifetimes;函数没有生命周期; references do.参考资料。 But you can have a reference to a function.但是您可以参考 function。 And you can have a lifetime as a parameter of a function.您可以将生命周期作为 function 的参数。

couldn't understand what lifetime 'a could mean here.无法理解 life 'a在这里可能意味着什么。

It just means it's a mutably borrowed parameter of closure with lifetime &'a .这只是意味着它是一个可变借用的闭包参数,带有生命周期&'a

In essence, async fn wrapper<F, Fut>(f: F) behaves the same, since &' a can be elided.本质上, async fn wrapper<F, Fut>(f: F)的行为相同,因为&' a可以省略。

It would be useful if, somewhere inside the function's code, you needed to say lifetime 'a will outlive some lifetime 'b .如果在函数代码中的某处需要说生命周期'a将比生命周期'b长,这将很有用。

For example:例如:

async fn wrapper<'a, 'b, F, Fut>(f: F, b: &'b Hi)
where
    Fut: Future<Output = ()>,
    F: FnOnce(&'a mut Hi) -> Fut,
    'a : 'b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM