Minimal reproducible code:
trait VecExt<T, R> {
fn map(&self, f: impl Fn(&T) -> R) -> Vec<R>;
}
impl<T, R> VecExt<T, R> for Vec<T> {
fn map(&self, f: impl Fn(&T) -> R) -> Vec<R> {
self.iter().map(f).collect()
}
}
struct A {
a: Vec<Option<String>>
}
impl A {
fn foo(&self) -> Vec<Option<&String>> {
self.a.map(|o| o.as_ref())
}
}
Getting error message:
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
When using self.a.iter().map(|o| o.as_ref()).collect()
, it compiles.
I've already read the explain document, but I still don't know why this error occurs and how to solve it.