Skip to content

Instantly share code, notes, and snippets.

@aneurysmjs
Created September 15, 2024 09:32
Show Gist options
  • Save aneurysmjs/5e07bb33d9a532abf516536b506f0fcf to your computer and use it in GitHub Desktop.
Save aneurysmjs/5e07bb33d9a532abf516536b506f0fcf to your computer and use it in GitHub Desktop.
Rust iterators
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