Created
March 12, 2016 06:01
-
-
Save anonymous/3275db726d36a53262c9 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains hidden or 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
#[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