Created
September 15, 2024 09:32
-
-
Save aneurysmjs/5e07bb33d9a532abf516536b506f0fcf to your computer and use it in GitHub Desktop.
Rust iterators
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub struct RectIter { | |
points: Vec<(f64, f64)>, | |
idx: usize, | |
} | |
impl Iterator for RectIter { | |
type Item = (f64, f64); | |
/* | |
* The reason why is mutable (&mut self), is that the only way to iterate through something, | |
* is that the underline iterator has to be mutable, bacause you have to be able to change | |
* the state until you are at the end. | |
*/ | |
fn next(&mut self) -> Option<Self::Item> { | |
if self.idx >= self.points.len() { | |
return None; | |
} | |
let point = self.points[self.idx]; | |
self.idx += 1; | |
return Some(point); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment