Ref<Option<T>> -> Option<Ref<T>>?

I am aware of cell.rs - source .

I have:

x: Ref<Option<T>>

I want:

y: Option<Ref<T>>

Is there a way to do this?

EDIT: Here, Ref refers to Ref in std::cell - Rust

1 Like

Something like this should work:

use std::cell::{Ref,RefCell};

fn transpose_ref_opt<T>(x:Ref<'_,Option<T>>)->Option<Ref<'_,T>> {
    if x.is_none() { None }
    else { Some(Ref::map(x, |x| x.as_ref().unwrap()))}
}
5 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.