Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the `cycle()` method exists if `ChunksMut` is never `Clone?`

Tags:

rust

The https://doc.rust-lang.org/std/slice/struct.ChunksMut.html has the cycle method: https://doc.rust-lang.org/std/iter/struct.Cycle.html#method.cycle that works only when Self: Clone

However, ChunksMut does not implement Clone, therefore I cannot do this:

fn main() {
    let a = &[1,2,3,4,5,6];
    let mut chunks = a.chunks_mut(2);
    let cycle = chunks.cycle();
    for c in cycle {
        
    }
}

Why the cycle() method exists if ChunksMut is never Clone?

like image 555
Margareth Reena Avatar asked Nov 18 '25 19:11

Margareth Reena


1 Answers

ChunksMut implements the Iterator trait.

impl<'a, T> Iterator for ChunksMut<'a, T>

And the cycle() comes from the default implementation of Iterator trait. cycle has a predicate(where Self: Clone) which restrict calling cycle on types that are not cloneable.

fn cycle(self) -> Cycle<Self>
where
    Self: Clone, 
like image 154
Abdul Niyas P M Avatar answered Nov 21 '25 15:11

Abdul Niyas P M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!