【发布时间】:2014-09-10 21:49:56
【问题描述】:
我在处理结构的生命周期参数时遇到问题。我不是 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 是通过移动传递的,所以它应该可以正常工作吗?
有什么想法吗?感谢大家的帮助。
【问题讨论】: