Skip to content

map_values

Returns a new map with the same keys and each value replaced by f(value).

use helpers4::map::map_values;

Cargo feature map (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features map

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.5", default-features = false, features = ["map"] }
pub fn map_values<K: Clone + Eq + Hash, V, W, S: BuildHasher>(
    map: &HashMap<K, V, S>,
    mut f: impl FnMut(&V) -> W,
) -> HashMap<K, W>
ParameterTypeDescription
map&HashMap<K, V, S>The map to transform.
fimpl FnMut(&V) -> WComputes the new value from each old value.

HashMap<K, W>

use helpers4::map::map_values;
use std::collections::HashMap;

let prices = HashMap::from([("apple", 2), ("pear", 3)]);
let doubled = map_values(&prices, |price| price * 2);
assert_eq!(doubled, HashMap::from([("apple", 4), ("pear", 6)]));

src/map/map_values.rs