Cannot borrow mutable more than once error

I would phrase it more like "there is no way to do what you've attempted in safe Rust". The way you "convince the compiler" is to use unsafe; the compiler then assumes you've upheld the required invariants. Rust unsafe is actually more dangerous than C, because the invariants are more strict. It's very easy to get it wrong, even if you're experienced with it. I guess I'm saying you got unlucky with your choice of Rust project, or at least picked a very challenging one to make efficient and correct.

That's a self-referential struct, as presented anyway, so there are still going to be challenges. I can think of relatively straightforward ways around it like leaking the memory (until destruction, unleaking with unsafe; or forever).

What you're describing though is a String interner. I've seen a couple articles on the topic somewhat recently; I think you should check them out and see how they solved it. I'm going to refer you to those instead of diving into it myself:

Here's one from @matklad, who I definitely trust to know what they're talking about. Among other things in that post they mention Miri; look into using Miri if you decide to try anything involving unsafe yourself.

Here's another from @CAD97 (also active on this forum) that investigated a collection of interning crates. You could check out those crates to see how they solved the problem.

Thanks - the simple solution proposed is what I had in mind. At this stage, I will probably just implement something like this so I can get on with the rest of the project. But still interested in other easy ways of doing this - I will look at more clever ways of doing it when I revisit this later.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.