I want to have one itereator over two ordered key-value sequences (as iterators) matching their keys against each other and yielding the equal ones. In my first attempt I got stuck after filtering both iterators and want to zip the filtered iterators. See the uncommented line (18) in the following gist or playground:
/// Takes two iterators over an ordered (key, value) sequence,
/// removing all entries which keys don't find a match in place
/// and zips the resulting filtered iterators afterwards.
///
/// Assumes the keys are in ascending order.
pub fn filter_zip<K, V, W, A, B>(a: &mut A, b: &mut B) // -> Iterator<Item=(K,(V, W))>
where K: Ord,
A: Iterator<Item=(K, V)>,
B: Iterator<Item=(K, W)>,
{
a.filter(|&(ref key_a,_)| {
let mut is_equal = false;
b.filter(|&(ref key_b,_)| {
if key_a == key_b {
is_equal = true;
true
} else {
key_a > key_b
}
});
is_equal
})
// .zip(b)
;
}
Uncommenting that line the compiler tells me I cant move b
in the call to zip(b)
beacause its borrowed. I know this makes sense at all but does anyone have an idea how to achieve my desired implementation?
Edit: how can I activate syntaxhighliting for code examples?