问题描述
我试过了:
use std::collections::HashMap;
struct Foo {
my_map: HashMap<String,String>,}
impl Foo {
fn bar(&self) -> dyn Iterator<Item = String> {
self.my_map
.values()
.into_iter()
.filter(|m| m.len() == 0)
.collect()
}
}
这会返回
error[E0277]: the size for values of type `(dyn Iterator<Item = String> + 'static)` cannot be kNown at compilation time
use std::collections::HashMap;
struct Foo {
my_map: HashMap<String,}
impl Foo {
fn bar(&self) -> Box<dyn Iterator<Item = String>> {
Box::new(
self.my_map
.values()
.into_iter()
.filter(|m| m.len() == 0)
.collect(),)
}
}
由于无法推断 Box
的类型而失败。
在尝试添加显式类型 arg 后:
use std::collections::HashMap;
struct Foo {
my_map: HashMap<String,}
impl Foo {
fn bar(&self) -> Box<dyn Iterator<Item = String>> {
Box::<dyn Iterator<Item = String>>::new(
self.my_map
.values()
.into_iter()
.filter(|m| m.len() == 0)
.collect(),)
}
}
它现在失败了:
error[E0599]: no function or associated item named `new` found for struct `Box<dyn Iterator<Item = String>>` in the current scope
--> src/lib.rs:10:45
|
10 | Box::<dyn Iterator<Item = String>>::new(
| ^^^ function or associated item not found in `Box<dyn Iterator<Item = String>>`
|
= note: the method `new` exists but the following trait bounds were not satisfied:
`dyn Iterator<Item = String>: Sized`
我对这门语言很陌生,所以不确定这里发生了什么。在这种情况下如何返回迭代器?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)