Skip to content

Commit

Permalink
feat: add fold
Browse files Browse the repository at this point in the history
  • Loading branch information
katopz committed Sep 18, 2023
1 parent 270aaf6 commit 719c021
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
- [AsRef](rust/r3/as-ref.md)
- [Callback](rust/r3/callback.md)
- [Async](rust/r3/async.md)
- [Iterators](rust/r3/iterators.md)
- [R2 - Expert](rust/r2/mod.md)
- [Hello Github Action](rust/r2/hello-github-action.md)
- [Hello Actix CloudRun](rust/r2/hello-actix-cloudrun.md)
Expand Down
25 changes: 25 additions & 0 deletions src/rust/r3/iterators.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Iterators

## How to sum value by key in object list?

```rust,editable
use std::collections::HashMap;
fn main() {
println!(
"{:?}",
vec![
("key1", 10),
("key2", 20),
("key1", 5),
("key3", 15),
("key2", 25),
]
.into_iter()
.fold(HashMap::new(), |mut acc, (key, value)| {
*acc.entry(key).or_insert(0) += value;
acc
})
);
}
```

0 comments on commit 719c021

Please sign in to comment.