diff options
| author | bors <bors@rust-lang.org> | 2013-06-01 21:31:36 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-06-01 21:31:36 -0700 |
| commit | 14c331053ea9d2661cfd9c81351f4b3815cf5e3d (patch) | |
| tree | 339ec3e8a10db3a80f908a2299b1161a3a146a9a /src/rt/rust_kernel.cpp | |
| parent | 96f6f29477cbe8ce0f779f493b243fc657b6e22e (diff) | |
| parent | 75f1b7f96fa4e91244a96ba92f615f3213d97519 (diff) | |
| download | rust-14c331053ea9d2661cfd9c81351f4b3815cf5e3d.tar.gz rust-14c331053ea9d2661cfd9c81351f4b3815cf5e3d.zip | |
auto merge of #6815 : kballard/rust/hashmap-insert_or_modify_with, r=erickt
`std::hashmap::HashMap.insert_or_update_with()` is basically the opposite
of `find_or_insert_with()`. It inserts a given key-value pair if the key
does not already exist, or replaces the existing value with the output
of the passed function if it does.
This is useful because replicating this with existing functionality is awkward, especially with the current borrow-checker. In my own project I have code that looks like
if match map.find_mut(&key) {
None => { true }
Some(x) => { *x += 1; false }
} {
map.insert(key, 0);
}
and it took several iterations to make it look this good. The new function turns this into
map.insert_or_update_with(key, 0, |_,x| *x += 1);
Diffstat (limited to 'src/rt/rust_kernel.cpp')
0 files changed, 0 insertions, 0 deletions
