在本地存储对象后无法确定处理引用的生命周期
Cannot figure out lifetimes for processing reference after locally storing object
我正在尝试构建类似这样的东西 (playground):
struct X<'a> {
s: &'a str,
}
struct Y<'a> {
list: Vec<X<'a>>,
}
impl<'r> Y<'r> {
fn process(&mut self, e: &'r X) {
// do stuff
}
fn add(&mut self, e: X<'r>) {
self.list.push(e);
let new = self.list.last().unwrap();
self.process(new)
}
}
概念上这似乎没问题:self
有一个列表,其中添加了一个东西,然后我可以对那个东西做一些进一步的处理,因为我知道这个东西应该只要 self
。但是,编译器不同意:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
--> src/main.rs:15:29
|
15 | let new = self.list.last().unwrap();
| ^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 13:5...
--> src/main.rs:13:5
|
13 | / fn add(&mut self, e: X<'r>) {
14 | | self.list.push(e);
15 | | let new = self.list.last().unwrap();
16 | | self.process(new)
17 | | }
| |_____^
note: ...so that reference does not outlive borrowed content
--> src/main.rs:15:19
|
15 | let new = self.list.last().unwrap();
| ^^^^^^^^^
note: but, the lifetime must be valid for the lifetime 'r as defined on the impl at 9:1...
--> src/main.rs:9:1
|
9 | impl<'r> Y<'r> {
| ^^^^^^^^^^^^^^
= note: ...so that the types are compatible:
expected &mut Y<'_>
found &mut Y<'r>
在我看来 new
显然应该和 self
一样长(尽管可能不如可变引用 &mut self
长?),所以我不明白问题在这里。
fn process(&mut self, e: &'r X) {
这个签名看起来很可疑,可能是错误的。请注意,您不是在此处为 X
指定生命周期,而是为了引用它。这可能不是你想要的。
所以事实证明我不明白 Vec
存储的引用的生命周期应该如何工作。我逐渐尝试通过可变引用将内容(X
的实例)存储到 self
中,同时,使用对这些实例的引用也将它们存储在 self
中。但是,对 Vec
的引用仅在 Vec
本身不可变时才稳定。
因此,解决方案是从 Y
中提取 Vec
(list
),在初始化我的 Y
对象之前完全初始化它,这样它就是在 Y
对象的整个生命周期内不可变。
我正在尝试构建类似这样的东西 (playground):
struct X<'a> {
s: &'a str,
}
struct Y<'a> {
list: Vec<X<'a>>,
}
impl<'r> Y<'r> {
fn process(&mut self, e: &'r X) {
// do stuff
}
fn add(&mut self, e: X<'r>) {
self.list.push(e);
let new = self.list.last().unwrap();
self.process(new)
}
}
概念上这似乎没问题:self
有一个列表,其中添加了一个东西,然后我可以对那个东西做一些进一步的处理,因为我知道这个东西应该只要 self
。但是,编译器不同意:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
--> src/main.rs:15:29
|
15 | let new = self.list.last().unwrap();
| ^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 13:5...
--> src/main.rs:13:5
|
13 | / fn add(&mut self, e: X<'r>) {
14 | | self.list.push(e);
15 | | let new = self.list.last().unwrap();
16 | | self.process(new)
17 | | }
| |_____^
note: ...so that reference does not outlive borrowed content
--> src/main.rs:15:19
|
15 | let new = self.list.last().unwrap();
| ^^^^^^^^^
note: but, the lifetime must be valid for the lifetime 'r as defined on the impl at 9:1...
--> src/main.rs:9:1
|
9 | impl<'r> Y<'r> {
| ^^^^^^^^^^^^^^
= note: ...so that the types are compatible:
expected &mut Y<'_>
found &mut Y<'r>
在我看来 new
显然应该和 self
一样长(尽管可能不如可变引用 &mut self
长?),所以我不明白问题在这里。
fn process(&mut self, e: &'r X) {
这个签名看起来很可疑,可能是错误的。请注意,您不是在此处为 X
指定生命周期,而是为了引用它。这可能不是你想要的。
所以事实证明我不明白 Vec
存储的引用的生命周期应该如何工作。我逐渐尝试通过可变引用将内容(X
的实例)存储到 self
中,同时,使用对这些实例的引用也将它们存储在 self
中。但是,对 Vec
的引用仅在 Vec
本身不可变时才稳定。
因此,解决方案是从 Y
中提取 Vec
(list
),在初始化我的 Y
对象之前完全初始化它,这样它就是在 Y
对象的整个生命周期内不可变。