Option::unwrap and references

I have a &Option<T> and I want to unwrap to a &T, where the borrow checker checks that our ref doesn't outlive the original ref. Is there a way to do this?

You could use as_ref and then unwrap/expect/etc

fn main() {
    let a = &Some(3);
    let b = a.as_ref();
    let c = b.unwrap();
}
2 Likes

Is this what your after (if @hellow does not fully meet your requirements.)

let original_ref: &Option<_> = &option;
let livein: &_;
fn limited<'a, T>(or: &'a &Option<T>) -> &'a T { or.as_ref().unwrap() }
livein = limited(&original_ref);

giving error on outlive use

1 Like

@jonh @hellow

Thanks for the help - it was just @hellow's as_ref solution that I needed.