Is Heavy Use of .clone() Normal in Rust?

297 points AbbreviationsNew3167 109 comments

Hey everyone,

I recently started learning Rust and have been following various tutorials and examples to get familiar with the language.

One thing I've noticed is that .clone() seems to be used quite frequently in many code examples (and tutorials) . This surprised me because I was under the impression that cloning data can be relatively expensive, especially when dealing with larger types.

So I'm wondering:

  • Why is .clone() used so often in Rust code?
  • Is the performance cost usually negligible in these cases?
  • Are beginners being taught patterns that favour simplicity over efficiency?
  • How do experienced Rust developers decide when cloning is acceptable and when it should be avoided?

I'd appreciate any insights or best practices regarding the use of .clone() in Rust.

Thanks!