value_or

Function value_or 

Source
pub fn value_or<K, V>(map: &HashMap<K, V>, key: &K, fallback: V) -> V
where K: Eq + Hash, V: Clone,
Expand description

Returns a value from a map for a given key, or a fallback value if the key doesn’t exist.

§Arguments

  • map - The map to get the value from
  • key - The key to look up
  • fallback - The value to return if the key doesn’t exist

§Returns

  • V - Either the value from the map or the fallback value

§Examples

use lowdash::value_or;
use std::collections::HashMap;

let mut map = HashMap::new();
map.insert("a", 1);

assert_eq!(value_or(&map, &"a", 42), 1);
assert_eq!(value_or(&map, &"b", 42), 42);