- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用结构的生命周期参数时遇到问题。我不是 100% 确定如何描述问题,但我创建了一个简单的案例来显示我的编译时错误。
struct Ref;
struct Container<'a> {
r : &'a Ref
}
struct ContainerB<'a> {
c : Container<'a>
}
trait ToC {
fn to_c<'a>(&self, r : &'a Ref) -> Container<'a>;
}
impl<'a> ToC for ContainerB<'a> {
fn to_c(&self, r : &'a Ref) -> Container<'a> {
self.c
}
}
我得到的错误是
test.rs:16:3: 18:4 error: method `to_c` has an incompatible type for trait: expected concrete lifetime, but found bound lifetime parameter 'a
test.rs:16 fn to_c(&self, r : &'a Ref) -> Container<'a> {
test.rs:17 self.c
test.rs:18 }
test.rs:16:48: 18:4 note: expected concrete lifetime is the lifetime 'a as defined on the block at 16:47
test.rs:16 fn to_c(&self, r : &'a Ref) -> Container<'a> {
test.rs:17 self.c
test.rs:18 }
error: aborting due to previous error
我已经尝试了很多变体,但就是无法编译这个东西。我在这里找到了另一篇文章 ( How to fix: expected concrete lifetime, but found bound lifetime parameter ),但似乎是在绕过问题而不是解决问题。我真的不明白为什么会出现这个问题。 &Ref 是通过 moves 传递的,所以它应该能正常工作?
有什么想法吗?感谢所有帮助。
最佳答案
让我们比较一下这两个定义。一、trait方法:
fn to_c<'a>(&self, r: &'a Ref) -> Container<'a>;
和实现:
fn to_c(&self, r: &'a Ref) -> Container<'a>;
看出区别了吗?后者没有 <'a>
。 <'a>
已在别处指定;它具有相同的名称这一事实并不重要:它完全是另一回事。
从功能上讲,您的特征定义表示返回的容器内部将有一个对 r
中的内容的引用,但 self
中没有任何内容。它可以在方法内部使用 self
,但它可能不会在返回值中存储对它的任何引用。
但是,您的方法定义使用的是 'a
,它将 r
和返回的 Container
的生命周期与 self
联系起来(也就是说,对象本身,而不是引用——&'ρ₁ T<'ρ₂>
中的 ρ2——这是一个微妙但有时很重要的区别), 而特征定义没有这种联系。
可以通过在实现中的方法定义中插入<'a>
来使两者匹配。但请记住,这是从 'a
隐藏 ContainerB<'a>
;它不是同一个 'a
!我们最好给它起另一个名字;为方便起见,我将以相反的方式进行更改,在 impl 而不是方法上更改它(两者都可以):
impl<'b> ToC for ContainerB<'b> {
fn to_c<'a>(&self, r: &'a Ref) -> Container<'a> {
self.c
}
}
但现在您当然遇到了问题:返回值的类型为 Container<'b>
(因为这是 c
中的字段 ContainerB<'b>
的类型),但您的签名需要 Container<'a>
(使用来自 r
的引用,而不是来自 self
的引用)。
解决它的一种方法是在特征定义和实现中将 &self
的生命周期指定为 'a
;在实现中,这将要求 'b
大于或等于 'a
(由于您已经成功地将生命周期为 'a
的引用引用到生命周期为 'b
的对象,并且该对象必须比引用长寿)等等由于子类型化('a
是 'b
的子类型),Container<'b>
将被安全地强制转换为 Container<'a>
。
这些人生大事,不熟是很难想的;但随着时间的推移,它们会变得很自然。
关于reference - Rust 生命周期错误预期具体生命周期但发现绑定(bind)生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24847331/
有谁知道蓝牙设备如何获取范围内可发现设备的设备 ID? 理想情况下,我正在寻找涉及蓝牙协议(protocol)最小实现的最简单解决方案。 一个起点会很好,我只是想创建一个设备,它可以以最小的功耗存储附
我有用于搜索Kibana中特定事件的查询,该查询与其他搜索一起保存,是否可以通过REST调用以编程方式更改它? 最佳答案 正如@Mohammad所说,所有与Kibana相关的元数据都存储在elasti
我正在使用带有这些注释的基本集成测试: @ExtendWith(SpringExtension::class) @SpringBootTest(classes = [SomeApplication::
以下是我的代码 HTML: Hello !! Javascript: $(function() { $('#content .child').click(function() {
我试图避免在每个 GDB session 中输入相同的命令。为此,我遵循了 rust discovery book 中的说明。但是当我通过 cargo run 运行程序时,程序没有像书中提到的那样工作
好的,我记得有一些命令可以放在 settings.py 文件中,这样基本上当您将 django 项目移动到另一个目录时,它就不会启动 foo-bar . 我知道我可以通过在它提到主目录的任何地方设置一
假设我正在制作一份注册表单。现在我希望它突出显示四个字段中的空白字段。现在我可以只执行一堆 if-else 语句,但这将花费很长时间。 假设我有以下代码: Javascript: if($firstn
我试图理解 C++ 中 regex 的逻辑 std::string s ("Ni Ni Ni NI"); std::regex e ("(Ni)"); std::smatch sm; std::re
运行时: vim /tmp/blah :q echo $? 我的退出状态为 1 .这破坏了包括 Git 在内的各种东西。如果我在没有 vimrc 的情况下运行 vim: vim -u NONE /tm
我无法通过“查找”来查找旧文件。我将我的发现链接到一个声明中,所有其他部分都运行良好。这是我所拥有的精简版。它搜索 $path 的目录树,并为找到的每个项目创建仅包含换行符的单独临时文件:所有文件、超
我已经多次看到这个问题,但没有一个答案对我有用。 我的 DotNet Core 应用程序失败 编码: public static void Main(string[] args) {
已解决见编辑 2 你好, 我一直在编写一个 Perl 程序来处理本地(专有)程序的自动升级(对于我工作的公司)。 基本上,它通过 cron 运行,不幸的是有内存泄漏(或类似的东西)。问题是泄漏只发生在
在 icCube 中创建到 Oracle 数据库的连接时,“选择现有数据库表”返回一个空的表列表。 连接很好,我可以查询模式创建 SQL 查询。 最佳答案 用户用作模式名称,但 Oracle 使用大写
我正在使用 VBA 循环遍历两个工作表上的行,如果它们匹配,则将工作表 2 中的行复制到工作表 1 中。 我的代码应该: 打开第二个工作簿 将所有信息复制到新工作表上的原始工作簿中 然后循环遍历原始工
当我尝试同步我的数据库时出现这个奇怪的错误: Unhandled rejection Error: Cyclic dependency found. roles is dependent of its
以编程方式发现 perl 模块具有的所有子例程的最佳方法是什么?这可以是一个模块、一个类(没有@EXPORT)或任何介于两者之间的东西。 编辑:下面的所有方法看起来都可以工作。我可能会在生产中使用 C
如何在“讨论”按钮左侧添加“共享”按钮。我希望该按钮与当前的“讨论”按钮具有相同的样式/颜色。 我从https://atmospherejs.com/joshowens/shareit添加了包 我将
我最近从 Netbeans 切换到 Eclipse,Eclipse 在我的项目中发现了许多 Netbeans 没有的语法错误,我不知道为什么。据可以看出,两个 IDE 都设置为使用 java 1.6。
我必须为我的项目设置一些不接受错误网址的规则。我为此使用正则表达式。 我的网址是“http://some/resource/location”。 此网址的开头、中间或结尾不应留有空格。 例如,这些空格
问题:鉴于作为 VMSS 的一部分启动的 N 个实例,我希望每个 Azure 实例上的应用程序代码能够发现其他对等实例的 IP 地址。我该怎么做? 总体目的是对实例进行集群,以提供主动被动 HA 或保
我是一名优秀的程序员,十分优秀!