DEV Community

Milo
Milo

Posted on

Cloning, how does it work?

Hey!
Recently, I have been faced with an error. I have a DasHap of a &str and a bool, and I have 2 functions, each in a loop, each in their own thread. I need to share this dashmap between both threads, so I wrapped it in Arc. I get a borrowing error, of course, and was suggested to clone outside of the thread because the closure is async move. But, then, if I update the not-cloned value, that won't reflect on the cloned-value, and to update the cloned-value, I have to reference it where the not-cloned value changes, which is in a thread, that would cause borrowing errors.
To sum up, having a value being cloned outside of a loop, requiring the original value to be updated, subsequently also requires the cloned-value to be update, but can't be done as if I update the cloned-value in thread 1, I can't use it in thread 2 because of borrowing errors.

Any help appreciated!

Discussion (0)