为什么需要绑定`T: 'a` 来存储引用`&'a T`? [英] Why is the bound `T: 'a` required in order to store a reference `&'a T`?

查看:18
本文介绍了为什么需要绑定`T: 'a` 来存储引用`&'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.

从这个意义上说,已经不可能获得 T 不超过 'a&'a T.因此额外的注解 (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?

链接:

推荐答案

这是格式良好规则的一部分.类型 &'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.

这个要求来自一个实现细节;在结构的内部,T'a 不一定像 &'a T 一样一起使用.格式良好的需求需要提升到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` 来存储引用`&amp;'a T`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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