其他分享
首页 > 其他分享> > rust iter3

rust iter3

作者:互联网

struct CountdownIterator(i32);

impl Iterator for CountdownIterator {
    type Item = i32;
    fn next(&mut self) -> Option<Self::Item> {
        self.0 -= 1;
        if self.0 < 0 {
            None
        } else {
            Some(self.0)
        }
    }
}

struct Countdown(i32);

impl IntoIterator for Countdown {
    type Item = i32;
    type IntoIter = CountdownIterator;
    fn into_iter(self) -> Self::IntoIter {
        CountdownIterator(self.0)
    }
}

impl<'a> IntoIterator for &'a Countdown {
    type Item = i32;
    type IntoIter = CountdownIterator;
    fn into_iter(self) -> Self::IntoIter {
        CountdownIterator(self.0)
    }
}

impl<'a> IntoIterator for &'a mut Countdown {
    type Item = i32;
    type IntoIter = CountdownIterator;
    fn into_iter(self) -> Self::IntoIter {
        CountdownIterator(self.0)
    }
}

fn main() {
    let c = Countdown(10);
    for i in &c {
        for j in &c {
            println!("({0}, {1})", i, j)
        }
    }
}

  

标签:iter3,i32,IntoIter,Countdown,CountdownIterator,type,self.0,rust
来源: https://www.cnblogs.com/pythonClub/p/16528224.html