Resolved: Cannot borrow `*self`. How to make the fn work without string cloning?

In this post, we will see how to resolve Cannot borrow *self. How to make the fn work without string cloning?

Question:

I don’t want to clone a string every time. How to get it working with borrowed string?
And yes, I really need the &mut self here.

Best Answer:

You can wrap your strings in Rc that way you can cheaply clone the Rc and have something owned so you don’t reference the original struct:
For mutable access to the underlying strings you can use Rc::get_mut or wrap further in RefCell or similar.
Solution to the original underspecified question:
The most straight forward solution is to just remove the String from Foo for the baz call:

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com