Crates.io | grouping_by |
lib.rs | grouping_by |
version | 0.2.2 |
source | src |
created_at | 2020-06-28 18:26:18.527006 |
updated_at | 2021-09-22 19:57:08.070669 |
description | A simple library which allows the user to group an iterator by various ways. |
homepage | |
repository | https://github.com/Davichet-e/grouping-by |
max_upload_size | |
id | 259163 |
size | 18,743 |
This small library provides users the possibility of grouping their iterators of various ways. It is still in development and therefore is not recommended for production code. There will be breaking changes constantly.
It is similar to Java Collectors.groupingBy
#[derive(Debug, PartialEq)]
struct Point {
x: i32,
y: i32,
}
let array: [Point; 4] = [
Point { x: 1, y: 2 },
Point { x: 1, y: 3 },
Point { x: 2, y: 2 },
Point { x: 2, y: 2 },
];
assert_eq!(
[
(1, vec![&Point { x: 1, y: 2 }, &Point { x: 1, y: 3 }]),
(2, vec![&Point { x: 2, y: 2 }, &Point { x: 2, y: 2 }])
]
.iter()
.cloned()
.collect::<HashMap<i32, Vec<&Point>>>(),
array.iter().grouping_by(|point| point.x)
);
// This returns for each year, the contract with the most days.
contracts.iter().grouping_by_max(
|contract| contract.date.year(), // Key of HashMap
|contract1, contract2| contract1.days.cmp(&contract2.days), // Comparator to get the max
) // Returns `HashMap<i32, Contract>`
Just import the trait (use grouping_by::GroupingBy;
) into your crate and use it on your iterators.