我正在尝试在FlatMap
中创建所有可能的物品对:
possible_children.clone().flat_map(|a| possible_children.clone().map(|b| (a,b)))
为了做到这一点,我正在尝试克隆一个FlatMap,我在文档中看到FlatMap结构体实现了clone方法。但是似乎不可能创建一个满足trait bounds的FlatMap。
这是我得到的错误:
error: no method named `clone` found for type `std::iter::FlatMap<std::ops::Range<u16>, _, [closure@src/main.rs:30:47: 33:27]>` in the current scope
--> src/main.rs:37:66
|
37 | possible_children.clone().flat_map(|a| possible_children.clone().map(|b| (a,b)))
| ^^^^^
|
= note: the method `clone` exists but the following trait bounds were not satisfied: `[closure@src/main.rs:30:47: 33:27] : std::clone::Clone`
看文档,我发现:
impl<I, U, F> Clone for FlatMap<I, U, F>
where F: Clone, I: Clone, U: Clone + IntoIterator, U::IntoIter: Clone
并且
impl<I, U, F> Iterator for FlatMap<I, U, F>
where F: FnMut(I::Item) -> U, I: Iterator, U: IntoIterator
看起来F
既受到Clone
特质的限制,又受到FnMut
特质的限制,但是不可能有东西同时实现FnMut
和Clone
。
文档中存在一个无法调用的方法似乎很奇怪,所以我一定漏掉了什么。
请问有人能为我澄清一下吗?
MVCE:
fn main() {
let possible_children = (0..10).flat_map(|x| (0..10).map(|y| (x,y)));
let causes_error = possible_children.clone().flat_map(|a|
possible_children.clone().map(|b| (a,b) )
).collect();
println!("{:?}",causes_error);
}
possible_children
的值是多少,导致出现了该错误? - Dogbertlet possible_children = (0..10).flat_map(|x| (0..10).map(|y| (x,y) ) );
- Michael Ratliff