New to Rust? Don’t make these common mistakes

Don’t sweat using .clone() to sidestep borrowing (at first)

When you’re writing your first Rust programs, the complexities of ownership and borrowing can be dizzying. If all you want to do is write a simple program that doesn’t need to be performant or hugely memory-optimized, Rust’s memory management might seem intrusive.

This isn’t always going to be true; in fact, your growth as a Rust developer depends on learning when memory management is essential. But in the very early stages of Rust-dom, when you’re still getting your sea legs inside the language’s syntax and tooling, that feature can feel like a burden.

One way to reduce your worry about borrowing—both now and later—is to clone objects rather than transfer ownership. Cloning creates a new instance of the same data but with a new, independent owner. The original instance keeps the original owner, so there are no issues with object ownership. And, as with the original object, the clone will be dropped automatically once it goes out of scope.

Donner Music, make your music with gear
Multi-Function Air Blower: Blowing, suction, extraction, and even inflation

Leave a reply

Please enter your comment!
Please enter your name here