【发布时间】:2020-01-29 03:29:36
【问题描述】:
pub struct FooStruct<'a> {
pub bars: Vec<&'a str>,
}
pub trait FooTrait<'a> {
fn getBars(&self) -> &'a Vec<&'a str>;
}
impl<'a> FooTrait<'a> for FooStruct<'a> {
fn getBars(&self) -> &'a Vec<&'a str> {
&self.bars // cannot infer an appropriate lifetime for borrow expression due to conflicting requirements
}
}
我不明白需求冲突来自哪里。 Afaik 没有冲突,因为只要 FooStruct 存在,一切都会存在。
【问题讨论】:
-
'a可能比FooStruct的生命周期更长。那么拥有&'a Vec<_>是不合理的,因为Vec不会持续整个'a。