问题描述
给出以下代码:
use std::iter::Iterator;
trait Sequence {
type SeqType: Iterator<Item = u32>;
fn seq(&self) -> Option<Self::SeqType>;
}
struct Doubler<'a>(Option<&'a [u32]>);
impl<'a> Sequence for Doubler<'a> {
type SeqType = Box<dyn Iterator<Item = u32>>;
// NOT WORKING
fn seq(&self) -> Option<Self::SeqType> {
self.0
.map(|v| Box::new(v.to_vec().into_iter().map(|x| x * 2)))
}
}
fn print_seq<S: Sequence>(seq: S) {
let v: Option<Vec<u32>> = seq.seq().map(|i| i.collect());
println!("{:?}",v);
}
fn main() {
let v = vec![1,2,3,4];
print_seq(Doubler(Some(&v)));
}
编译器会抱怨:
error[E0308]: mismatched types
--> src/main.rs:16:9
|
15 | fn seq(&self) -> Option<Self::SeqType> {
| --------------------- expected `std::option::Option<std::boxed::Box<(dyn std::iter::Iterator<Item = u32> + 'static)>>` because of return type
16 | / self.0
17 | | .map(|v| Box::new(v.to_vec().into_iter().map(|x| x * 2)))
| |_____________________________________________________________________^ expected trait object `dyn std::iter::Iterator`,found struct `std::iter::Map`
|
= note: expected enum `std::option::Option<std::boxed::Box<(dyn std::iter::Iterator<Item = u32> + 'static)>>`
found enum `std::option::Option<std::boxed::Box<std::iter::Map<std::vec::IntoIter<u32>,[closure@src/main.rs:17:58: 17:67]>>>`
但是它可以通过替换seq
来实现:
fn seq(&self) -> Option<Self::SeqType> {
fn convert(s: &[u32]) -> Box<dyn Iterator<Item = u32>> {
Box::new(s.to_vec().into_iter().map(|x| x * 2))
}
self.0.map(convert)
}
可以here对代码进行测试。
唯一的区别是在失败的示例中使用了闭包,而其他示例使用了(命名为?)函数,但是逻辑是相同的。似乎是一辈子的问题,但还没弄清楚。此外,Option::map
应该会急切地消耗该值,然后应立即使用闭包。
为什么关闭示例失败?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)