Lifetime error: cached member function

Thanks!
About the Cow thing:
Basically because MyData is really large and I want to avoid copying until neccessary:

And following your 'a suggestion, I now get that I am returning a temporary.
Which I kinda get, probably because of the RefCell returning the Ref object to do the runtime refcounting, but I can also not return a Ref object.


struct Container<'a> {
    data: Vec<MyData<'a>>,
    cache_first_element: RefCell<Option<MyData<'a>>>,
}

impl<'a> Container<'a> {
    fn new() -> Self {
        Container {
            data: vec![],
            cache_first_element: RefCell::new(None),
        }
    }
}

use std::iter::IntoIterator;

impl<'a> Container<'a> {
    /// I want to store a copy locally in Container, but on `first_element` calls return a ref to it
    /// instead of moving/ copying
    fn first_element(&self) -> &MyData<'a> {
        match self.cache_first_element.borrow().as_ref() {
            Some(d) => d,
            None => &self
                .cache_first_element
                .replace(
                    self.data
                        .iter()
                        .take(1)
                        .cloned()
                        .map(|r| -> MyData<'a> {
                            MyData {
                                name: r.name.to_owned(),
                                numbers: r.numbers.to_owned(),
                            }
                        })
                        .next(),
                )
                .unwrap(),
        }
    }
}