Why is not an iterator an flat_map
I think these two methods are equivalent, but there is an error. What is the reason? Is there a better way to express it?
pub fn create_pair() -> () {
let vec_num = vec![1, 2, 3];
let vec_num2 = &vec_num;
let all = &vec_num
.iter()
.flat_map(move |a| vec_num2.iter().map(move |b| [a, b]))
.collect::<Vec<_>>();
println!("{:?}", all);
}
[[1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3]]
pub fn create_pair() -> () {
let vec_num = vec![1, 2, 3];
let all = &vec_num
.iter()
.flat_map(move |a| &vec_num.iter().map(move |b| [a, b]))
.collect::<Vec<_>>();
println!("{:?}", all);
}
error[E0277]: `&std::iter::Map<std::slice::Iter<'_, {integer}>, [closure@src/main.rs:5:48: 5:63 a:_]>` is not an iterator
--> src/main.rs:5:10
|
5 | .flat_map(move |a| &vec_num.iter().map(move |b| [a, b]))
| ^^^^^^^^ `&std::iter::Map<std::slice::Iter<'_, {integer}>, [closure@src/main.rs:5:48: 5:63 a:_]>` is not an iterator
|
= help: the trait `std::iter::Iterator` is not implemented for `&std::iter::Map<std::slice::Iter<'_, {integer}>, [closure@src/main.rs:5:48: 5:63 a:_]>`
= note: required because of the requirements on the impl of `std::iter::IntoIterator` for `&std::iter::Map<std::slice::Iter<'_, {integer}>, [closure@src/main.rs:5:48: 5:63 a:_]>`
error[E0599]: no method named `collect` found for struct `std::iter::FlatMap<std::slice::Iter<'_, {integer}>, &std::iter::Map<std::slice::Iter<'_, {integer}>, [closure@src/main.rs:5:48: 5:63 a:_]>, [closure@src/main.rs:5:19: 5:64 vec_num:_]>` in the current scope
--> src/main.rs:6:10
|
6 | .collect::<Vec<_>>();
| ^^^^^^^ method not found in `std::iter::FlatMap<std::slice::Iter<'_, {integer}>, &std::iter::Map<std::slice::Iter<'_, {integer}>, [closure@src/main.rs:5:48: 5:63 a:_]>, [closure@src/main.rs:5:19: 5:64 vec_num:_]>`
|
= note: the method `collect` exists but the following trait bounds were not satisfied:
`&std::iter::Map<std::slice::Iter<'_, {integer}>, [closure@src/main.rs:5:48: 5:63 a:_]>: std::iter::IntoIterator`
which is required by `std::iter::FlatMap<std::slice::Iter<'_, {integer}>, &std::iter::Map<std::slice::Iter<'_, {integer}>, [closure@src/main.rs:5:48: 5:63 a:_]>, [closure@src/main.rs:5:19: 5:64 vec_num:_]>: std::iter::Iterator`
`std::iter::FlatMap<std::slice::Iter<'_, {integer}>, &std::iter::Map<std::slice::Iter<'_, {integer}>, [closure@src/main.rs:5:48: 5:63 a:_]>, [closure@src/main.rs:5:19: 5:64 vec_num:_]>: std::iter::Iterator`
which is required by `&mut std::iter::FlatMap<std::slice::Iter<'_, {integer}>, &std::iter::Map<std::slice::Iter<'_, {integer}>, [closure@src/main.rs:5:48: 5:63 a:_]>, [closure@src/main.rs:5:19: 5:64 vec_num:_]>: std::iter::Iterator`
from Recent Questions - Stack Overflow https://ift.tt/2Gn7ufa
https://ift.tt/eA8V8J
Comments
Post a Comment