Skip to content

Instantly share code, notes, and snippets.

Created March 12, 2016 06:01
Show Gist options
  • Save anonymous/3275db726d36a53262c9 to your computer and use it in GitHub Desktop.
Save anonymous/3275db726d36a53262c9 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
#[derive(Debug)]
struct S {
v: Vec<i32>,
}
impl S {
fn objects_at(&self, pos: i32) -> ObjectsIter {
ObjectsIter { it: self.v.iter(), pos: pos }
}
}
struct ObjectsIter<'a> {
it: std::slice::Iter<'a, i32>,
pos: i32,
}
impl<'a> Iterator for ObjectsIter<'a> {
type Item = i32;
fn next (&mut self) -> Option<Self::Item> {
while let Some(p) = self.it.next() {
if self.pos == *p {
return Some(*p);
}
}
None
}
}
fn main() {
let s = S { v: vec![1, 2, 3, 2] };
println!("{:?}", s);
for v in s.objects_at(2) {
println!("{}", v);
}
println!("{:?}", s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment