Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::collections::HashSet;
- use std::iter;
- #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
- struct Id(pub usize);
- type View = Vec<Id>;
- type IdSet = HashSet<Id>;
- fn find_stuff(view: View, ids: &IdSet) -> impl Iterator<Item=Id> {
- let mut ids = ids.iter();
- iter::from_fn(move || {
- while let Some(id) = ids.next() {
- if view.contains(id) {
- return Some(*id);
- }
- }
- None
- })
- }
- fn set_from_seed(id: Id) -> IdSet {
- let mut result = IdSet::with_capacity(1);
- result.insert(id);
- result
- }
- fn main() {
- let view = vec![Id(0), Id(1), Id(2), Id(3)];
- for x in find_stuff(view, &set_from_seed(Id(2))) {
- println!("{:?}", x);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement