问题描述
struct Struct0 {
s1: Struct1,v: Vec<Box<dyn Tr>>
}
impl Struct0 {
fn a(&mut self) {
self.v = self.s1.func();
}
}
struct Struct1 {}
impl<'a> Struct1 {
fn func(&'a self) -> Vec<Box<dyn Tr + 'a>> {
vec![Box::new(Struct2 { some_reference: &self } )]
}
}
trait Tr {
//...
}
struct Struct2<'a> {
some_reference: &'a Struct1
}
impl Tr for Struct2<'_> {
//...
}
fn main() {
}
这会出现以下错误:
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> src/main.rs:8:26
|
8 | self.v = self.s1.func();
| ^^^^
|
note: first,the lifetime cannot outlive the anonymous lifetime #1 defined on the method body at 7:5...
--> src/main.rs:7:5
|
7 | / fn a(&mut self) {
8 | | self.v = self.s1.func();
9 | | }
| |_____^
note: ...so that reference does not outlive borrowed content
--> src/main.rs:8:18
|
8 | self.v = self.s1.func();
| ^^^^^^^
= note: but,the lifetime must be valid for the static lifetime...
note: ...so that the expression is assignable
--> src/main.rs:8:18
|
8 | self.v = self.s1.func();
| ^^^^^^^^^^^^^^
= note: expected `std::vec::Vec<std::Boxed::Box<(dyn Tr + 'static)>>`
found `std::vec::Vec<std::Boxed::Box<dyn Tr>>`
我想告诉Rust编译器,返回值的生存期应该是Struct1的生存期。该向量(及其值)应与调用该方法的结构一样长。我怎么说呢?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)