当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Rust Iterator.cycle用法及代码示例


本文简要介绍rust语言中 core::iter::Iterator.cycle 的用法。

用法

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

无休止地重复一个迭代器。

迭代器不会在 None 停止,而是从头开始重新开始。再次迭代后,又会从头开始。然后再次。然后再次。永远。

例子

基本用法:

let a = [1, 2, 3];

let mut it = a.iter().cycle();

assert_eq!(it.next(), Some(&1));
assert_eq!(it.next(), Some(&2));
assert_eq!(it.next(), Some(&3));
assert_eq!(it.next(), Some(&1));
assert_eq!(it.next(), Some(&2));
assert_eq!(it.next(), Some(&3));
assert_eq!(it.next(), Some(&1));

相关用法


注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 core::iter::Iterator.cycle。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。