Returning immutable references with Option, RefCell

For the second one you just need to transform the Option with as_ref.

fn get(&self) -> &str {
    self.string.as_ref().unwrap()
}

For the other one it's a little more complicated because you have to return the RefCell's guard.

fn get(&self) -> Ref<str> {
    Ref::map(self.string.borrow(), |borrow| {
        borrow.as_ref().unwrap().as_str()
    })
}
3 Likes