Alternatives to Rc<RefCell>
reddit.com·1w·
Discuss: r/rust
🧡Svelte
Preview
Report Post

I am working on a code analyzer and ran into a problem where I need a data structure that can hold itself as mutable. I could not use something like .clone() as I might need to assign a variable from one of the scopes "higher up" in the tree. Example:

fn some_fn() { // Scope 1
let mut x = 43;
if true { // Scope 2
x = 12; // Assigns x from scope 1
}
}

When I analyze something with a body, a function or an if statement, for instance, I call an analyze_node function for each node in the body. Since this is in a for-loop, I can’t simply borrow mutably. Thus I ended up just wrapping the scope in an Rc<RefCell>.

Personally, I am not a fan of this solution. Is there any other way to solve this?

Similar Posts

Loading similar posts...