This code doesn't compile:
fn drain_some<'a>(
vals: &'a mut Vec<Vec<i32>>,
inds: &'a [usize],
) -> impl Iterator<Item = i32> + 'a {
inds.iter().flat_map(|i| vals[*i].drain(..))
}
Because (as I understand it) the draining iterator holds a mutable reference (vals
) that was captured by a FnMut
's, and you can't return that from the FnMut
. But it seems like it should be possible, since the closure doesn't actually get called again until the draining iterator is dropped.
Is there another approach to this that works?