I'm trying to return an array iterator of an array in an Option
from the closure passed to a filter_map
so I can flatten it afterwards.
Unfortunately, rustc
produces the following error:
cannot return value referencing local variable `res`
returns a value referencing data owned by the current function
main.rs(3, 5): returns a value referencing data owned by the current function
main.rs(3, 10): `res` is borrowed here
for the minimal example:
fn demo<'a>() -> Option<impl Iterator + 'a> {
let res = [1,2];
Some(res.into_iter())
}
Though the complete code I'm trying to make work is this:
fn generate_next<'a>(prev: &'a [u32]) -> impl Iterator + 'a {
let mut counter = 1_u32;
prev.windows(2).filter_map(move |window| {
if window[0] == window[1] {
counter+=1;
None
} else {
let res = [counter, window[0]];
counter=1;
Some(res.into_iter())
}
}).flatten()
}
Both produce the same error for the Some(...)
part.
If I understand correctly, the code should work, because the into_iter()
method consumes the array and produces an iterator from it. Some
should then take ownership of the iterator via a move. Why does rustc think that I'm borrowing res
here?
I'm also open to other ways to implement the generate_next
function.
question from:
https://stackoverflow.com/questions/66049801/return-iterator-of-an-array-wrapped-in-an-option 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…