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...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help