Why does ops :: Range <T> implement Copy, even if T is Copy?

Recently, I wanted to write type storage parameters for a 3D projection:

use std::ops::Range;

#[derive(Clone, Copy)]
struct CamProj {
    /// Near and far plane
    proj_range: Range<f32>,
    /// Field of view
    fov: cgmath::Rad<f32>,     // `Rad` derives `Copy`
    /// Width divided by height
    aspect_ratio: f32,       
}

However, I got this error:

error[E0204]: the trait `Copy` may not be implemented for this type
 --> <anon>:3:21
  |
3 |     #[derive(Clone, Copy)]
  |                     ^^^^
...
6 |         proj_range: Range<f32>,
  |         ---------------------- this field does not implement `Copy`

So, Range<T>never realizes Copy, even if T- Copyfor example f32. Why is this? I thought it Range<T>would be just a pair of Ts? So he could implement Copy?

+6
source share
1 answer

Range<T> , Copy , . , , :

for x in it {  // a *copy* of the iterator is used here
    // ..
}

match it.next() {  // the original iterator is used here
    // ..
}

:

fn main() {
    let stream = "Hello, world!".chars().cycle();
    for _ in 0..10 {
        let chunk: String = stream.take(3).collect();
        println!("{}", chunk);
    }
}

, : Rust

, clone


, Copy Range . :

, ( ) /

. :

+7

Source: https://habr.com/ru/post/1016560/


All Articles