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

问题描述 投票:3回答:1

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

rust
1个回答
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);
}
© www.soinside.com 2019 - 2024. All rights reserved.