Skip to content

Instantly share code, notes, and snippets.

@Davichet-e
Created August 18, 2020 20:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Davichet-e/cbf8a203aa84b852a78a3611131d3749 to your computer and use it in GitHub Desktop.
Save Davichet-e/cbf8a203aa84b852a78a3611131d3749 to your computer and use it in GitHub Desktop.
A small example of grouping-by crate.
// 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>`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment