I have a function that takes in a Vec<String>
value. I want to use this function on values contained inside my_ref
, so I need to extract a Vec<String>
out of a Rc<RefCell<Vec<String>>>
.
I thought I could do this by dereferencing a borrow of my my_ref
, just like I would for a Rc<RefCell<f32>>>
or Rc<RefCell<i32>>>
value:
use std::cell::RefCell;
use std::rc::Rc;
fn main() {
let my_ref = Rc::from(RefCell::from(vec![
"Hello 1".to_string(),
"Hello 2".to_string(),
]));
let my_strings: Vec<String> = *my_ref.borrow();
let count = count_strings(my_strings);
}
fn count_strings(strings: Vec<String>) -> usize {
strings.len()
}
But doing so results in a dereferencing error:
error[E0507]: cannot move out of dereference of `Ref<'_, Vec<String>>`
cannot move out of dereference of `Ref<'_, Vec<String>>`
move occurs because value has type `Vec<String>`, which does not implement the `Copy` trait
So then, how do I properly extract a Vec<String>
from a Rc<RefCell<Vec<String>>>
?