I have the following program (playground). While the code marked "WORKS" works, using "map" to seems more natural. But I am getting an error when I try that ("cannot return value referencing function parameter info").
How do I use map in such situation or is there another easier way to express the same logic?
struct Info<'a> {
name: &'a str,
}
struct Foo {}
impl Foo {
fn test(&self) {
let info_opt1: Option<Info> = None;
// WORKS:
let _processed_info1 = match info_opt1 {
Some(ref info) => Some(self.process(&info)),
None => None
};
let info_opt2: Option<Info> = None;
// ERROR: returns a value referencing data owned by the current function
let _processed_info2 = info_opt2.map(|info| self.process(&info));
}
fn process<'a>(&self, info: &'a Info<'_>) -> &'a str {
info.name
}
}