用单个撇号表示的 Rust 类型是什么? [英] What are the Rust types denoted with a single apostrophe?

查看:59
本文介绍了用单个撇号表示的 Rust 类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Rust 中遇到过许多用一个撇号表示的类型:

'静态'r'一种

那个撇号有什么意义?也许它是引用的修饰符(&)?特定于引用的通用类型?我不知道这方面的文档藏在哪里.

解决方案

这些是 Rust 的命名生命周期.

引用自 Rust 编程语言:

<块引用>

Rust 中的每个引用都有一个生命周期,即该引用有效的范围.大多数时间生命周期是隐式和推断的,就像大多数时间类型是推断的一样.类似于当我们必须注释类型因为可能有多种类型时,在某些情况下引用的生命周期可能以几种不同的方式相关,因此 Rust 需要我们使用通用生命周期参数来注释关系,以便它可以确保运行时使用的实际引用肯定是有效的.

<块引用>

生命周期注释不会改变任何引用的时间参与直播.以同样的方式,函数可以接受任何类型,当签名指定了一个泛型类型参数,函数可以接受当签名指定泛型时具有任何生命周期的引用寿命参数.生命周期注释所做的是将多个相互引用的生命周期.

生命周期注解有一个稍微不寻常的语法:生命周期参数必须以撇号 ' 开头.的名字生命周期参数通常都是小写的,就像泛型类型一样,他们的名字通常很短.'a 是大多数人使用的名称默认值.生命周期参数注解在 & 之后引用,并且一个空格将生命周期注解与生命周期注解分开引用的类型.

换句话说,生命周期近似于引用指向的数据有效的执行跨度.Rust 编译器会保守地推断最短的生命周期是安全的.如果你想告诉编译器一个引用的生命周期比最短的估计要长,你可以命名它,例如说输出引用与给定的输入引用具有相同的生命周期.

'static 生命周期是一个特殊的生命周期,是所有生命周期中最长的——在程序的持续时间内.一个典型的例子是字符串文字",它在程序/模块的生命周期内始终可用.

您可以从此幻灯片获得更多信息,从第 29 张幻灯片开始.

Rust 中的生命周期 还深入讨论了生命周期.

I've encountered a number of types in Rust denoted with a single apostrophe:

'static
'r
'a

What is the significance of that apostrophe? Maybe it's a modifier of references (&)? Generic typing specific to references? I've no idea where the documentation for this is hiding.

解决方案

These are Rust's named lifetimes.

Quoting from The Rust Programming Language:

Every reference in Rust has a lifetime, which is the scope for which that reference is valid. Most of the time lifetimes are implicit and inferred, just like most of the time types are inferred. Similarly to when we have to annotate types because multiple types are possible, there are cases where the lifetimes of references could be related in a few different ways, so Rust needs us to annotate the relationships using generic lifetime parameters so that it can make sure the actual references used at runtime will definitely be valid.

Lifetime annotations don’t change how long any of the references involved live. In the same way that functions can accept any type when the signature specifies a generic type parameter, functions can accept references with any lifetime when the signature specifies a generic lifetime parameter. What lifetime annotations do is relate the lifetimes of multiple references to each other.

Lifetime annotations have a slightly unusual syntax: the names of lifetime parameters must start with an apostrophe '. The names of lifetime parameters are usually all lowercase, and like generic types, their names are usually very short. 'a is the name most people use as a default. Lifetime parameter annotations go after the & of a reference, and a space separates the lifetime annotation from the reference’s type.

Said another way, a lifetime approximates the span of execution during which the data a reference points to is valid. The Rust compiler will conservatively infer the shortest lifetime possible to be safe. If you want to tell the compiler that a reference lives longer than the shortest estimate, you can name it, saying that the output reference, for example, has the same lifetime as a given input reference.

The 'static lifetime is a special lifetime, the longest lived of all lifetimes - for the duration of the program. A typical example are string "literals" that will always be available during the lifetime of the program/module.

You can get more information from this slide deck, starting around slide 29.

Lifetimes in Rust also discusses lifetimes in some depth.

这篇关于用单个撇号表示的 Rust 类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆