Why doesn't `Range<T: Copy>` implement `Copy`?

This has confused me before. std::ops::Range<T> is a struct containing two T. If it were annotated #[derive(Copy)], then for example, Range<usize> would be Copy. But for some reason it's not, and so when I store a Range in a struct that should be able to be Copy, it can't. I'm curious if there is some reason for this?

Because it's an iterator, and iterators are generally not made Copy because that makes them do surprising things in for loops (and generally anything that "takes ownership").

There keep being conversations about splitting up the "interval from a to b" and "iterator from a to b" into different types (RangeInclusive is also a bit awkward here for the former use, because it's bigger than one would like for that use), but nothing's happened yet.

3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.