rust iter3

python;gutter:true; struct CountdownIterator(i32);</p> <p>impl Iterator for CountdownIterator { type Item = i32; fn next(&mut self) -> Option { self.0 -= 1; if self.0 < 0 { None } else { Some(self.0) } } }</p> <p>struct Countdown(i32);</p> <p>impl IntoIterator for Countdown { type Item = i32; type IntoIter = CountdownIterator; fn into_iter(self) -> Self::IntoIter { CountdownIterator(self.0) } }</p> <p>impl IntoIterator for &'a Countdown { type Item = i32; type IntoIter = CountdownIterator; fn into_iter(self) -> Self::IntoIter { CountdownIterator(self.0) } }</p> <p>impl IntoIterator for &'a mut Countdown { type Item = i32; type IntoIter = CountdownIterator; fn into_iter(self) -> Self::IntoIter { CountdownIterator(self.0) } }</p> <p>fn main() { let c = Countdown(10); for i in &c { for j in &c { println!("({0}, {1})", i, j) } } }

Original: https://www.cnblogs.com/pythonClub/p/16528224.html
Author: CrossPython
Title: rust iter3

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/15778/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

最近整理资源【免费获取】:   👉 程序员最新必读书单  | 👏 互联网各方向面试题下载 | ✌️计算机核心资源汇总