什么是IMPH特征+ '生命周期

发布于 2025-02-12 09:24:05 字数 595 浏览 1 评论 0原文

我正在阅读 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白况 2025-02-19 09:24:06

什么是Impl特征 +'lifetime在这里?

它是 smpp thrait ,在这里特别用作摘要返回类型

IMPH特征

语法

  Implatraittype:imp typeparambounds
ImpltraittypeOneBound:IMP
 

impl特征提供了指定未命名但具体类型的方法
实施特定的特征。它可以在两个地方出现:
参数位置(它可以充当匿名类型参数
功能)和返回位置(可以作为抽象的地方
返回类型)。

 特质特质{}

//参数位置:匿名类型参数
fn foo(arg:inpl属性){
}

//返回位置:抽象返回类型
fn bar() - &gt; IMPT特征{
}
 

其中 typeparambounds 特质和终身界限

特质和终身界限

语法

  typeparambounds:
   typeparambound( + typeparambound)* +?

Typeparambound:
      终生|特征

特征:
      ?无量吗? Typepath
   | (?? forliftimes?typepath)

终身界:
   (Lifetime +)*生命?

寿命 :
      Lifetime_or_label
   | '静止的
   | _ _
 

特别是注意到typeparambounds的语法是一个或选择的几个typeparambound s的组合代码>特征或lifetime(边界)。

指定与 +语法的多个性状边界详细描述我们可能会结合多个特征,但适用于寿命边界(语法允许的位置)。有人可能会争辩说,本节也应提及终身界限。

what is impl Trait + 'lifetime here?

It is an Impl trait, here particularly used as an abstract return type

Impl trait

Syntax

ImplTraitType : impl TypeParamBounds
ImplTraitTypeOneBound : impl TraitBound

impl Trait provides ways to specify unnamed but concrete types that
implement a specific trait. It can appear in two sorts of places:
argument position (where it can act as an anonymous type parameter to
functions), and return position (where it can act as an abstract
return type).

trait Trait {}

// argument position: anonymous type parameter
fn foo(arg: impl Trait) {
}

// return position: abstract return type
fn bar() -> impl Trait {
}

Where the grammar of TypeParamBounds allows both trait and lifetime bounds

Trait and lifetime bounds

Syntax

TypeParamBounds :
   TypeParamBound ( + TypeParamBound )* +?

TypeParamBound :
      Lifetime | TraitBound

TraitBound :
      ?? ForLifetimes? TypePath
   | ( ?? ForLifetimes? TypePath )

LifetimeBounds :
   ( Lifetime + )* Lifetime?

Lifetime :
      LIFETIME_OR_LABEL
   | 'static
   | '_

Particularly noting that the grammar of TypeParamBounds is one or optionally several TypeParamBounds combined, each of which in turn is either TraitBound or Lifetime (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.

当梦初醒 2025-02-19 09:24:06

并不完全熟悉,但是从逻辑上讲,这是有道理的:

函数返回未来,完成后,返回字节。 实际执行异步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. Technically x 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 as x

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文