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/
转载文章受原作者版权保护。转载请注明原作者出处!