如何解决返回包装在 Option 中的数组的迭代器
我试图从传递给 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()
会产生与调用 iter()
相同的结果,即引用上的迭代器。这是 Rust 标准库中的一个不幸的问题。
您可以通过使用 std::iter::once
创建 counter
和 window[0]
的迭代器,然后将它们 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()
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。