Difference between copied and cloned on Rust iterators

Should I just always use cloned() as it will work in the more general case?

Often, the Rust optimiser will be able to figure out that a clone can be replaced with a faster copy. However, this isn't guaranteed, so use copied() where you can, to make sure you end up with the fastest binary.


I managed to find (thanks to Peter!) this pull request which explains the original reasoning behind adding copied() in addition to cloned()...

The intent of copied is to avoid accidentally cloning iterator elements after doing a code refactoring which causes a structure to be no longer Copy. This is a relatively common pattern, as it can be seen by calling rg --pcre2 '[.]map[(][|](?:(\w+)[|] [*]\1|&(\w+)[|] \2)[)]' on Rust main repository. Additionally, many uses of cloned actually want to simply Copy, and changing something to be no longer copyable may introduce unnoticeable performance penalty.