Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chunk_by returns only the last value when collected #978

Open
buinauskas opened this issue Aug 1, 2024 · 2 comments
Open

chunk_by returns only the last value when collected #978

buinauskas opened this issue Aug 1, 2024 · 2 comments

Comments

@buinauskas
Copy link

buinauskas commented Aug 1, 2024

use std::collections::HashMap;
use itertools::Itertools;

fn main() {
    let numbers = vec![1, 2, 3, 4, 5, 6];

    let chunks: HashMap<i32, Vec<i32>> = numbers
        .into_iter()
        .chunk_by(|number| number % 2)
        .into_iter()
        .map(|(k, v)| (k, v.collect()))
        .collect();

    let expectation = HashMap::from([
        (0, vec![2, 4, 6]),
        (1, vec![1, 3, 5]),
    ]);

    assert_eq!(chunks, expectation);
}

This is the full code and I would expect chunk_by to group elements by the result of closure and build a map out of it, however when running the equality assertion panics with:

assertion `left == right` failed
  left: {0: [6], 1: [5]}
 right: {1: [1, 3, 5], 0: [0, 2, 4, 6]}
  • stable-aarch64-apple-darwin unchanged - rustc 1.80.0 (051478957 2024-07-21)

Is that by design? The method example explicitly shows a loop when it iterates over the chunked results and does not call collect on the iterator.

@phimuemue
Copy link
Member

phimuemue commented Aug 1, 2024 via email

@buinauskas
Copy link
Author

In the past I used group_by and it has a deprecation warning telling me to use chunk_by, so that's what I did.

But apparently, there's into_group_map_by which does exactly what I'm after. This only left me with an open question whether that functionality is by design and whether it's expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants