HashMap::insert
pub fn insert(K k, V v) -> bool Import
Source: use collections::map::{HashMap}; src/collections/map.hy Stores v under k, replacing any value already there.
Returns true when k is new and false when an existing value was replaced.
Called on a HashMap value as value.insert(…).
Parameters#
| Name | Type |
|---|---|
k | K |
v | V |
Returns#
bool
Contracts#
Checked according to --contracts (see Contracts).
ensures !result || self.len == old(self.len) + 1
ensures result || self.len == old(self.len)
Examples#
use collections::map::{HashMap};
use tests::assert;
let m: HashMap<string, int> = HashMap::new();
assert!(m.insert("a", 1));
assert!(!m.insert("a", 2));
assert!(m.get_or("a", 0) == 2);
See also#
HashMap: A hash map from keys of typeKto values of typeV.HashMap::with_capacity: Creates an empty map with room for aboutcapbuckets before it grows.HashMap::new: Creates an empty map.HashMap::size: Returns the number of entries.HashMap::is_empty: Returnstruewhen the map has no entries.HashMap::capacity: Returns the number of buckets.HashMap::clear: Removes every entry, keeping the bucket count.HashMap::keys: Returns the keys, in insertion order.