I'm trying to learn how to implement a custom iterator.
In this example I have a Color
struct with r
, g
, and b
fields.
I want to be able to iterate over those values.
The code below is my start, but I'm not sure how to finish it.
use std::iter::Iterator;
struct Color {
r: u8,
g: u8,
b: u8
}
impl Iterator for Color {
type Item = u8;
// How can I keep track of the last value returned?
fn next(&mut self) -> Option<Self::Item> {
// Return one of these in this sequence:
// Some(self.r)
// Some(self.g)
// Some(self.b)
// None
}
}
fn main() {
let color = Color {r: 100, g: 0, b: 150};
for v in color.iter() {
println!("{}", v);
}
}