]> piware.de Git - learn-rust.git/blobdiff - src/lib.rs
Iterators
[learn-rust.git] / src / lib.rs
index fe3b71b286640b29919d465a9fd25f55a002b42b..b7e5eb8f1df11116f8d94fd31fb1afe29491d087 100644 (file)
@@ -85,3 +85,26 @@ where
         }
     }
 }
+
+pub struct Counter5 {
+    count: u32
+}
+
+impl Counter5 {
+    pub fn new() -> Counter5 {
+        Counter5 { count: 0 }
+    }
+}
+
+impl Iterator for Counter5 {
+    type Item = u32;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        if self.count < 5 {
+            self.count += 1;
+            Some(self.count)
+        } else {
+            None
+        }
+    }
+}