Short Answer
No, it's not possible to create a Rc<T> from an Rc<Option<T>> that leaves the latter still existing. It is possible to create an Rc<&T> however, from a Rc<Option<T>>, while still leaving the latter variable existing.
Long Answer
If you're trying to create a new Rc<T> that owns the T inside the Rc<Option<T>>, you will have to consume the original Rc<Option<T>>. You also can't have multiple instances of the Rc<Option<T>>, because then you're moving the shared value while pointers still exist, which is very unsafe.
But there is a way to do this safely! Using Rc::try_unwrap, you can attempt to move the value out, but this will return an error if multiple instances of the original Rc exist.
Keep in mind you also have to handle the scenario where Option<T> ends up being None.
Here's an example of this:
let rc_option: Rc<Option<T>> = Rc::new(Some(value));
match Rc::try_unwrap(rc_option) {
Ok(option) => {
match option {
Some(t) => {
let ok_value: Rc<T> = Rc::new(t);
// Do something with ok_value
}
None => {
// Do something else here
}
}
}
Err(rc_option) => {
// There are multiple owners, do something else here
}
}
If you wanted to preserve the original, you could do this:
match &*rc_option {
Some(ref t) => {
let ok_ref: Rc<&T> = Rc::new(t);
}
None => { /* Do something else, there's no internal value */ }
}
EDIT: As Chronial mentioned, do note that the ok_ref cannot outlive rc_option (because it's a reference to rc_option), which may not be what you want to happen.