我希望能够在 Parent
中存储一个名为 Child
的结构,其中 Child
包含对父级的引用。
如果我将 Child
结构直接放在父级中,就可以了,如下所示:
struct Parent<'s> {
cache: RefCell<Vec<Child<'s>>>
}
但是如果我将 Vec
移动到一个单独的结构中,那么它将无法编译并出现生命周期错误。
struct Parent<'s> {
cache: RefCell<Cache<'s>>
}
struct Cache<'s> {
children: Vec<Child<'s>>
}
可以使此示例与单独的结构一起使用吗?
这是full working code ,编译得很好。当将 children
移动到单独的结构中时,它 fails .
我对问题的分析:
当Parent
直接包含children
时,的
与Parent
结构体的作用域具有相同的生命周期本身,因此我可以在 Parent
上调用采用 &'s self
的方法。
当Parent
包含Cache
(其中包含children
)时,的
的生命周期与Cache
结构体,它是在 Parent
之前创建的,因此不可能在 Parent
上调用采用 &'s self
的方法。尝试这样做会出现错误
<anon>:33:15: 33:16 error: `p` does not live long enough
<anon>:33 let obj = p.create_object();
^
<anon>:30:48: 38:2 note: reference must be valid for the block suffix following statement 0 at 30:47...
<anon>:30 let cache = Cache { children: Vec::new() }; // the lifetime `'s` is essentially from this line to the end of the program
<anon>:31 let mut p = Parent { cache: RefCell::new(cache) }; // although the Parent instance was created here, 's still refers to the lifetime before it
<anon>:32 // this fails because p doesn't live long enough
<anon>:33 let obj = p.create_object();
我需要一种方法将的
缩短到Parent
的范围,而不是Cache
的范围。
免责声明: 这个问题与我之前提出的问题( https://stackoverflow.com/questions/32579518/rust-lifetime-error-with-self-referencing-struct?noredirect=1#comment53014063_32579518 )非常相似,该问题被标记为重复。我已经通读了答案,我相信我已经超越了这个范围,因为我可以得到正确的引用的生命周期(如我的第一个示例所示)。我再次问这个(现在略有不同)问题,因为我现在有一个有效的具体示例,以及一个无效的示例。我确信用一个结构可以完成的事情可以用两个结构完成,对吗?
最佳答案
您可以通过在同一个 let
绑定(bind)中定义 Cache
和 Parent
来强制它们具有相同的生命周期,从而使其编译。
fn main() {
let (cache, mut p);
cache = Cache { children: Vec::new() };
p = Parent { cache: RefCell::new(cache) };
let obj = p.create_object();
let c1 = Child { parent: &p, data: 1 };
p.cache.borrow_mut().children.push(c1);
}
在这里,我们本质上是声明一个解构的元组,然后初始化它。我们无法直接在 let
绑定(bind)上初始化元组:
let (cache, mut p) = (Cache { children: Vec::new() }, Parent { cache: RefCell::new(cache) });
因为p
的初始化程序引用了cache
,但该名称直到let
语句结束时才定义。单独的初始化之所以有效,是因为编译器跟踪哪些变量被初始化;如果交换分配的顺序,您将收到编译器错误:
<anon>:31:38: 31:43 error: use of possibly uninitialized variable: `cache` [E0381]
<anon>:31 p = Parent { cache: RefCell::new(cache) };
关于struct - 尝试将自引用数据拆分为单独的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32674398/