什么是IMPH特征+ '生命周期
我正在阅读 async book 。在有一个我不熟悉的语法代码
fn foo_expanded<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
async move { *x }
}
段未来&lt; output = u8&gt; +'a ,什么是Impl特征 +'Lifetime
在这里?
更新:我更多地问它是什么,而不是终身逻辑解释。官方文档的定义将是 非常感谢。
I am reading the async book. In section async lifetimes
there is a code snippet whose grammar I am not familiar with:
fn foo_expanded<'a>(x: &'a u8) -> impl Future<Output = u8> + 'a {
async move { *x }
}
In impl Future<Output = u8> + 'a
, what is impl Trait + 'lifetime
here?
Update: I am more asking what it is instead of the lifetime logic explanation. A definition from the official doc will be
much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它是 smpp thrait ,在这里特别用作摘要返回类型
其中
typeparambounds
特质和终身界限特别是注意到
typeparambounds
的语法是一个或选择的几个typeparambound
s的组合代码>特征或lifetime
(边界)。指定与 +语法的多个性状边界详细描述我们可能会结合多个特征,但适用于寿命边界(语法允许的位置)。有人可能会争辩说,本节也应提及终身界限。
It is an Impl trait, here particularly used as an abstract return type
Where the grammar of
TypeParamBounds
allows both trait and lifetime boundsParticularly noting that the grammar of
TypeParamBounds
is one or optionally severalTypeParamBound
s combined, each of which in turn is eitherTraitBound
orLifetime
(bounds).Specifying Multiple Trait Bounds with the + Syntax describe in some detail that we may combine more than one trait bound, but the same applies for lifetime bounds (where the grammar allows it). One could argue that the section should also mention lifetime bounds.
并不完全熟悉,但是从逻辑上讲,这是有道理的:
函数返回未来,完成后,返回字节。 实际执行异步FN时,它将移动
x
,这是具有生命周期的参考。从技术上讲,X 可以在未来完成之前删除。为了防止这种情况Not entirely familiar, but logically it makes sense:
The function returns a Future that, when completed returns a byte. When the async fn is actually executed, it moves
x
, which is a reference with a lifetime. Technicallyx
could be dropped before the Future is completed. To prevent this the lifetime trait guarantees that the return value of the function lives at least as long asx