So I came recently by terms of inspecting other peoples code to this two beats, but could not find a good tutorial/explanation of them. Now, from the docs I sort of understand (and please correct me if I'm wrong I need to know this) that std::rc::Rc behaves similarly (if not identical) to std::shared_ptr in C++ and Weak as the std::weak_ptr. So far so good, I think I'm following. Now as far as I understand, since RC has the method borrw_mut which gives us a mutable reference to the object. Now I don't quite understand from the docs example - std::rc - Rust, why we need the RefCell and what it is achieving and what is more how it is achieving on the background? And unfrotunately I did not found a good resource on line for that.
Just to give some background on the code I was looking at I have (will simplify for the post):
struct ParseState {
max_err_pos: usize,
expected: ::std::collections::HashSet<&'static str>,
context: ::std::rc::Rc<::std::cell::RefCell<ContextObjects>>,
}
struct ContextObjects {
dummy: (),
graph_ctx: ComputeGraph,
}
And then I have the following part of code, given that state: &mut ParseState
let _context = state.context.clone();
let mut _context = &mut *_context.borrow_mut();
let graph = &mut _context.graph_ctx;
I can't get my head around the second line, what exactly is the second _context
and what is the point of it and why if I try to return *graph
if the signature is to return ComputeGraph
rust gives me cannot move out of borrowed content
?