为什么需要绑定"T:'a"来存储参考“& a" T? [英] Why is the bound `T: 'a` required in order to store a reference `&'a T`?
问题描述
给出以下代码:
struct RefWrapper<'a, T> {
r: &'a T,
}
...编译器抱怨:
错误:参数类型
T
的寿命可能不够长
error: the parameter type
T
may not live long enough
考虑添加一个显式的生命周期绑定T: 'a
,以使引用类型&'a T
不会超出其指向的数据.
consider adding an explicit lifetime bound T: 'a
so that the reference type &'a T
does not outlive the data it points at.
我已经多次看到此错误,到目前为止,我只是听了编译器,一切正常.但是,想一想,我不明白为什么为什么要写T: 'a
.
I've seen this error multiple times already and so far I just listened to the compiler and everything worked out fine. However, thinking more about it, I don't understand why I have to write T: 'a
.
据我了解,已经已经不可能获得这样的参考.具有&'a T
意味着存在一个类型为T
的对象,该对象至少存在'a
.但是我们不能在该对象中存储任何指向寿命短于'a
的数据的引用.这将已经导致编译器错误.
As far as I understand, it is already impossible to get such a reference. Having &'a T
implies that there is an object of type T
that lives for at least 'a
. But we can't store any references in said object which point to data having a shorter lifetime than 'a
. This would already result in a compiler error.
从这个意义上说,已经不可能获得&'a T
,其中T
不超过'a
.因此,不需要附加注释(T: 'a
).
In that sense it is already impossible to get a &'a T
where T
does not outlive 'a
. Thus the additional annotation (T: 'a
) shouldn't be necessary.
我是对的吗?我是不是错了,如果是的话:如果不需要T: 'a
,我该如何破坏代码?
Am I right? Am I wrong and if yes: how could I break code, if T: 'a
would not be required?
链接:
- RFC introducing the syntax
- Another maybe related RFC
推荐答案
这是格式规则的一部分.类型&'a T
仅在T: 'a
("T超过'a"时才是格式正确的;之所以是必需的,因为我们有一个引用,我们可以在范围'a
期间访问该引用; T
中的指向值)至少在该范围内也需要有效.)
This is part of the well-formedness rules. The type &'a T
is only well-formed if T: 'a
("T outlives 'a"; it is required because we have a reference which we can access during the scope 'a
; the pointed-to value in T
needs to be valid for at least that scope, too).
struct RefWrapper<'a, T>
是泛型类型,它表示您可以输入生存期'x
和类型U
并获得RefWrapper<'x, U>
类型.但是,除非满足要求T: 'a
,否则这种类型不一定格式正确或什至没有实现.
struct RefWrapper<'a, T>
is a generic type and it says you can input a lifetime 'x
and a type U
and get a RefWrapper<'x, U>
type back. However, this type is not necessarily well-formed or even implemented unless the requirement T: 'a
is respected.
此要求来自实现细节;不一定要像&'a T
一样在结构的内部使用T
和'a
.需要将格式良好性要求提升到RefWrapper
结构的公共接口,以便即使内部实现不是必需的,形成RefWrapper<'_, _>
类型的要求也是公共的.
This requirement comes from an implementation detail; it's not necessarily so that T
and 'a
are used together like &'a T
in the struct's internals. The well formedness requirement needs to be promoted to the public interface of the RefWrapper
struct, so that the requirements of forming a RefWrapper<'_, _>
type are public, even if the internal implementation is not.
(在其他地方,同样的要求T: 'a
会再次出现,但隐含:
(There are other places where the same requirement T: 'a
comes back but is implict:
pub fn foo<'a, T>(x: &'a T) { }
我们发现了一个区别:这里的&'a T
类型也是公共api的一部分.)
we spot a difference: here the type &'a T
is part of the public api, too.)
这篇关于为什么需要绑定"T:'a"来存储参考“& a" T?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!