如何在没有for循环的情况下使用Itertools group_by迭代器方法?

时间:2017-12-19 11:01:40

标签: rust

Itertools::group_by是一种迭代器方法,每次关键函数更改时都会生成一个新组。提供的示例显示了如何将它与for循环一起使用,但在迭代器链中使用输出GroupBy结构似乎非常麻烦,除非我误解了某些内容:

let data = vec![1, 3, -2, -2, 1, 0, 1, 2];

// example from docs
for (key, group) in &data.into_iter().group_by(|elt| *elt >= 0) {
    assert_eq!(4, group.sum::<i32>().abs());
}

// usage in an iterator method chain
data.iter()
    .group_by(|elt| **elt >= 0)
    .into_iter()
    .map(|bool, group| (bool, group.collect::<Vec<i32>>()))
    .collect::<Vec<(bool, Vec<i32>)>>();

第二个例子无法编译:

error[E0619]: the type of this value must be known in this context
  --> src/main.rs:16:35
   |
16 |         .map(|bool, group| (bool, group.collect::<Vec<i32>>()))
   |                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0599]: no method named `collect` found for type `std::iter::Map<itertools::Groups<'_, bool, std::slice::Iter<'_, i32>, [closure@src/main.rs:14:19: 14:35]>, [closure@src/main.rs:16:14: 16:63]>` in the current scope
  --> src/main.rs:17:10
   |
17 |         .collect::<Vec<(bool, Vec<i32>)>>();
   |          ^^^^^^^
   |
   = note: the method `collect` exists but the following trait bounds were not satisfied:
           `std::iter::Map<itertools::Groups<'_, bool, std::slice::Iter<'_, i32>, [closure@src/main.rs:14:19: 14:35]>, [closure@src/main.rs:16:14: 16:63]> : std::iter::Iterator`
           `&mut std::iter::Map<itertools::Groups<'_, bool, std::slice::Iter<'_, i32>, [closure@src/main.rs:14:19: 14:35]>, [closure@src/main.rs:16:14: 16:63]> : std::iter::Iterator`

error[E0593]: closure takes 2 arguments but 1 argument is required
  --> src/main.rs:16:10
   |
16 |         .map(|bool, group| (bool, group.collect::<Vec<i32>>()))
   |          ^^^ ------------------------------------------------- takes 2 arguments
   |          |
   |          expected closure that takes 1 argument

我不确定我理解这个错误,但我认为编译器希望闭包的group参数具有这种显式类型:

itertools::Group<'_, bool, std::slice::Iter<'_, i32>, [closure@src\main.rs:26:15: 26:31]>

没有人想在他们的代码中,所以我希望我误解了。

问题是,是否可能,以及如何在没有for循环的情况下使用group_by

1 个答案:

答案 0 :(得分:8)

传递给map的闭包有错误的参数个数(两个而不是一个:改为使用元组);错误信息当然不是很有帮助:))

此外,我不会将bool用作变量名称,您需要在收集整数引用之前克隆它们(由于iter()into_iter())。

Playground

extern crate itertools;

use itertools::Itertools;

fn main() {
    let data = vec![1, 3, -2, -2, 1, 0, 1, 2];

    let groups = data.iter()
        .group_by(|elt| **elt >= 0)
        .into_iter()
        .map(|(ge0, group)| (ge0, group.cloned().collect()))
        .collect::<Vec<(bool, Vec<i32>)>>();
    println!("{:?}", groups);
}