Don’t sweat utilizing .clone() to sidestep borrowing (at first)
If you’re writing your first Rust packages, the complexities of possession and borrowing might be dizzying. If all you need to do is write a easy program that doesn’t should be performant or vastly memory-optimized, Rust’s reminiscence administration might sound intrusive.
This isn’t at all times going to be true; in reality, your progress as a Rust developer will depend on studying when reminiscence administration is crucial. However within the very early levels of Rust-dom, if you’re nonetheless getting your sea legs contained in the language’s syntax and tooling, that function can really feel like a burden.
One method to scale back your fear about borrowing—each now and later—is to clone objects slightly than switch possession. Cloning creates a brand new occasion of the identical information however with a brand new, impartial proprietor. The unique occasion retains the unique proprietor, so there aren’t any points with object possession. And, as with the unique object, the clone will probably be dropped mechanically as soon as it goes out of scope.