【发布时间】:2017-05-03 09:58:11
【问题描述】:
谁能说出以下代码中的生命周期错误是什么? (从我的实际代码中简化)我自己查看过它,但我不知道出了什么问题或如何解决它。当我尝试添加Cell 时出现问题,但我不确定为什么。
use std::cell::Cell;
struct Bar<'a> {
bar: &'a str,
}
impl<'a> Bar<'a> {
fn new(foo: &'a Foo<'a>) -> Bar<'a> { Bar{bar: foo.raw} }
}
pub struct Foo<'a> {
raw: &'a str,
cell: Cell<&'a str>,
}
impl<'a> Foo<'a> {
fn get_bar(&self) -> Bar { Bar::new(&self) }
}
编译错误是
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
--> src/foo.rs:15:32
|
15 | fn get_bar(&self) -> Bar { Bar::new(&self) }
| ^^^^^^^^
【问题讨论】: