I have a function of a third party library that needs ownership of a variable. Unfortunately this variable is inside a Rc<RefCell<Option<Foo>>>
.
My code looks simplified like this:
use std::cell::RefCell;
use std::rc::Rc;
pub struct Foo {
val: i32,
}
fn main() {
let foo: Rc<RefCell<Option<Foo>>> = Rc::new(RefCell::new(Some(Foo { val: 1 })));
if let Some(f) = foo.into_inner() {
consume_foo(f);
}
}
fn consume_foo(f: Foo) {
println!("Foo {} consumed", f.val)
}
error[E0507]: cannot move out of an `Rc`
--> src/main.rs:11:22
|
11 | if let Some(f) = foo.into_inner() {
| ^^^ move occurs because value has type `std::cell::RefCell<std::option::Option<Foo>>`, which does not implement the `Copy` trait
I tried to use std::mem::replace(...)
from How can I swap in a new value for a field in a mutable reference to a structure?:
fn main() {
let mut foo: Rc<RefCell<Option<Foo>>> = Rc::new(RefCell::new(Some(Foo { val: 1 })));
let mut foo_replaced = std::mem::replace(&mut foo.into_inner(), None);
if let Some(f) = foo_replaced.take() {
consume_foo(f);
}
}
error[E0507]: cannot move out of an `Rc`
--> src/main.rs:11:51
|
11 | let mut foo_replaced = std::mem::replace(&mut foo.into_inner(), None);
| ^^^ move occurs because value has type `std::cell::RefCell<std::option::Option<Foo>>`, which does not implement the `Copy` trait
I can't figure out how to do this properly.