EDIT: Not a duplicate. Please read the entire post.
Say I have a struct Foo<T>
:
struct Foo<T> {
data: T
}
which is used by an implementation that works with objects of type Rc<RefCell<Foo<T>>>
. I need to return a reference to the data inside of a Foo<T>
:
fn get(&self, some_arg) -> &T {
// ... some code ...
// foo is of type Rc<RefCell<Foo<T>>>
&foo.borrow().data // this doesn't work
}
But the Ref<'b, T>
returned by foo.borrow()
is only alive for the scope of get()
, so the compiler complains. I want to be able to do this:
// bar has the get() method above
let val = bar.get(some_arg); // bind val to a &T
// do stuff with val
How do I do this?
EDIT: I've already read all other SO posts that could be relevant. This is not a duplicate. The other posts suggested by Shepmaster all have different types (not Rc<RefCell<Foo<T>>>
): 1) There is no Rc
-wrapped member in Foo
. 2) There is not even RefCell
usage in the second "duplicate" post.