Hi,
(see playground).
I am trying to modify the elements of an iterator using a capturing closure.
The closure captures the variable myvec
, which is derived from the function argument input
.
However, there is a compile error ("returns a reference to a captured variable which escapes the closure body").
I suppose that the compile error is related to the lifetime of myvec
as using input
in the closure instead of myvec
works (even though this is oversimplifies my actual problem).
I guess that I want to tie myvec
to the lifetime of input
(since myvec
should survive until the iterator is completely used up (although I believed that move
would make sure of this already)) but my attempts did not work.
Interestingly, unfolding the call to bar
(commented last line of foo
) also works, which is pretty unclear to me. Also here, I would expect the same error message. Maybe it is because the lifetime annotations foo and bar are not tied together...?
fn bar<'a, A>(vec: &'a Vec<&'a A>, index: usize) -> Vec<&'a A> {
vec![vec[index]]
}
fn foo<'a>(domain: &'a Vec<&'a u32>) -> impl Iterator<Item=Vec<&'a u32>> {
// compiles and works: but myvec 'computation' is too much simplified
//let myvec = domain;
// compile error: returns a reference to a captured variable which escapes the closure body
let myvec = domain.clone();
return vec![&0_usize, &1, &2].into_iter().map(move |&x| bar(&myvec, x));
//return vec![&0_usize, &1, &2].into_iter().map(move |&x| vec![myvec[x]]);
}
#[test]
fn test() {
assert_eq!(
foo(&vec![&1_u32, &2, &3]).collect::<Vec<_>>(),
vec![vec![&1_u32], vec![&2], vec![&3]]);
}