impl<'a> Owner<'a> {
fn get_resource(&self) -> Option<Box<dyn ResourceTrait<'a>>> {
match &self.doc {
Some(doc) => {
Some(Box::new(TirdpartResource {
data: doc.get_page()
}))
},
None => Option::None
}
}
}
Box
这种堆上内存的生命周期需要显式标注。
方案1 让'_='a
:
fn get_resource(&'a self) -> Option<Box<dyn ResourceTrait<'a> + 'a>>
方案2 让'_ >= 'a
:
fn get_resource<'b: 'a>(&'b self) -> Option<Box<dyn ResourceTrait<'a> + 'b>>
https://rustcc.cn/article?id=ae00e464-b11a-4b7a-88a8-d7fb4d1a9e37