gpt4 book ai didi

rust - 如果 `cycle()` 永远不是 `ChunksMut`,为什么存在 `Clone?` 方法

转载 作者:行者123 更新时间:2023-12-04 16:35:43 26 4
gpt4 key购买 nike

https://doc.rust-lang.org/std/slice/struct.ChunksMut.html有循环方法:https://doc.rust-lang.org/std/iter/struct.Cycle.html#method.cycle仅在 Self: Clone

时有效

但是,ChunksMut 没有实现Clone,因此我不能这样做:

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 {

}
}

如果 ChunksMut 永远不会 Clone,为什么存在 cycle() 方法?

最佳答案

ChunksMut implements the Iterator trait .

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

还有 cycle() comes from Iterator 特性的默认实现。 cycle 有一个谓词(where Self: Clone)限制对类型为 not cloneable 的调用 cycle .

fn cycle(self) -> Cycle<Self>
where
Self: Clone,

关于rust - 如果 `cycle()` 永远不是 `ChunksMut`,为什么存在 `Clone?` 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70110155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com