Many iterators implement the Clone
trait, but none of them implement the Copy
trait. At least for simple iterators like std:slice::Iter
, the Clone
implementation looks like a memcpy anyway, so why isn't Copy
also implemented?
Asked
Active
Viewed 659 times
9

Shepmaster
- 388,571
- 95
- 1,107
- 1,366

Perseids
- 12,584
- 5
- 40
- 64
1 Answers
10
From PR #20790:
This PR also makes iterator non-implicitly copyable, as this was source of subtle bugs in the libraries. You can still use
clone()
to explictly copy the iterator.

DK.
- 55,277
- 5
- 189
- 162
-
2Could you provide an example of one of these subtle bugs? – Shepmaster Sep 01 '15 at 11:08
-
2@Shepmaster: I can't think of any. I assume it had something to do with iterators being passed into `for` loops, getting modified, but leaving the *original* iterator unmodified. – DK. Sep 01 '15 at 11:15
-
3I found [one example](https://github.com/rust-lang/rust/pull/27186#issuecomment-123390413), validating your memory. – Shepmaster Sep 01 '15 at 22:30