Return 包含在选项中的数组的迭代器

Return Iterator of an array wrapped in an Option

我正在尝试 return 一个 Option 中数组的数组迭代器从闭包传递给 filter_map 以便之后我可以展平它。 不幸的是,rustc 产生以下错误:

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

最小的例子:

fn demo<'a>() -> Option<impl Iterator + 'a> {
    let res = [1,2];
    Some(res.into_iter())
}

虽然我正在努力工作的完整代码是这样的:

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()
}

两者都为 Some(...) 部分产生相同的错误。

如果我理解正确,代码应该可以工作,因为 into_iter() 方法使用数组并从中生成迭代器。 Some 然后应该通过移动获得迭代器的所有权。为什么 rustc 认为我在这里借 res

我也愿意采用其他方式来实现 generate_next 功能。

调用 into_iter() on an array produces the same result as calling iter(),即对引用的迭代器。这是 Rust 标准库中的一个不幸问题。

你可以通过 std::iter::once to create iterators of counter and window[0] and then chain 它们一起使用来完成你想要的:

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 counter_iter = std::iter::once(counter);
                let window_iter = std::iter::once(window[0]);
                counter = 1;
                Some(counter_iter.chain(window_iter))
            }
        })
        .flatten()
}

playground