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);
1 Like