Rust中的迭代器

mac2022-06-30  35

和闭包一样,练代码

struct Counter { count: u32, } impl Counter { fn new() -> Counter { Counter {count: 0 } } } impl Iterator for Counter { type Item = u32; fn next(&mut self) -> Option<Self::Item> { self.count += 1; if self.count < 6 { Some(self.count) } else { None } } } #[test] fn using_other_iterator_trait_methods() { let sum: u32 = Counter::new().zip(Counter::new().skip(1)) .map(|(a, b)| a * b) .filter(|x| x % 3 == 0) .sum(); assert_eq!(18, sum); }

转载于:https://www.cnblogs.com/aguncn/p/11438455.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)