Hello,
I'm running into an issue with interior mutability and Opntion.
Consider the following code (or playground)
use std::collections::HashMap;
use std::cell::{RefCell, Ref};
fn main() {
let map = RefCell::new(HashMap::new());
map.borrow_mut().insert("age", 14);
fn myget(map: &RefCell<HashMap<String, String>>) -> Ref<Option<&String>> {
Ref::map(map.borrow(), |inner| {
&inner.get("age")
})
}
}
This code leads to following error:
error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements
--> src/main.rs:10:20
|
10 | &inner.get("age")
| ^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 9:32...
--> src/main.rs:9:32
|
9 | Ref::map(map.borrow(), |inner| {
| ________________________________^
10 | | &inner.get("age")
11 | | })
| |_________^
note: ...so that reference does not outlive borrowed content
--> src/main.rs:10:14
|
10 | &inner.get("age")
| ^^^^^
note: but, the lifetime must be valid for the anonymous lifetime defined on the function body at 8:19...
--> src/main.rs:8:19
|
8 | fn myget(map: &RefCell<HashMap<String, String>>) -> Ref<Option<&String>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...so that the expression is assignable
--> src/main.rs:9:9
|
9 | / Ref::map(map.borrow(), |inner| {
10 | | &inner.get("age")
11 | | })
| |__________^
= note: expected `Ref<'_, Option<&String>>`
found `Ref<'_, Option<&String>>`
Is there a way to resutnr a Option<Ref> or Ref<Option>?
Thanks for the help.